AngularJS 1 : $http always return status code -1 when request fails

浪子不回头ぞ 提交于 2019-12-11 07:17:01

问题


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?

来源:https://stackoverflow.com/questions/46993510/angularjs-1-http-always-return-status-code-1-when-request-fails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!