Search code examples
javascriptangularjserror-handlingdeferred

Not getting status error code in angular js promise


I would like to know the status code for the error in this function. I currently get this: GET http://localhost:3000/latest-live?lu=1448920013000 net::ERR_EMPTY_RESPONSE

Anyone know if I am handling my error function wrong? I know why I am getting the error, but to better handle the error I would like to know what the status code is (EX: status code 404 not found).

app.factory('getSpreadsheetData', ['$q', '$http', function($q, $http) {

return function getData() { 
    var deferred = $q.defer();

    var req = $http({
        url: 'http://localhost:3000/latest-live',
        method: 'GET',
        cache: false
    });

    req.success(function(object) {
        var data = object.data;
        deferred.resolve(data);
    });

    req.error(function(err) {
        deferred.reject(err);
    });

    return deferred.promise;
}
}]);

Solution

  • Usually that error is thrown, if there is definitly no reponse from the server you are trying to reach in your URL.

    Make sure that localhost is actually known to your system and something is running at port 3000 that can return something to your client.

    Make sure that the route is known on your server /latest-live and that it didn't crash before returning any information.

    First thing I would do is to check, if the URL is actually returning something, when you try to call it in your browser.

    Update: Status Codes

    Usually the status code is in the response, so you might want to check, if there is anything in err or response, depending on what function is called:

    response.status – Number – HTTP status code
    response.statusText – String – HTTP status text
    

    Best regards