AngularJS 1 : $http always return status code -1 w

2019-08-12 18:18发布

问题:

I would like to intercept 500 error and higher.

I get inspired by:

  • angular doc
  • AngularJs - $http.get request, response with -1 status code
  • and other similar questions

this is the ServiceErrorInterceptor

app.factory('ServerErrorInterceptor',
[ '$q',
  function ($q) {
    return {
      'requestError': function(rejection) {
        console.log( rejection );
         return $q.reject(rejection);
      },
        responseError: function (rejection) {
            console.log( rejection );
           return $q.reject(rejection);
        }
      };
    }
  ]);

which always returns

Object { data: null, status: -1, headers: headersGetter/<(), config: Object, statusText: "", xhrStatus: "error" }

while [Network] bowser tab shows clearly 400, 405, 500, 503 ... depending of the case.

How can I get the http error code?