Angular.js $http intercept “net::ERR_CONNECTION_REFUSED”

一个人想着一个人 提交于 2019-11-29 02:07:10

问题


I'm trying to write a generic error handler for my website using $http's interceptors but they don't seem to be able to do what I want to do.

I placed interceptors on 'response' and 'responseError' but they never get called when the server is offline/not reponding (net::ERR_CONNECTION_REFUSED). I understand why this happens, there's no response to intercept.

I'd like to know if there's a generic way of catching these errors, other than listening to the $httpPromise's error callback for each request.


回答1:


You can probably check the status of the ResponseError. When an API is offline that is 0 (until Angular 1.3.18) or -1 (since Angular 1.3.19):

angular.module("services.interceptor", arguments).config(function($httpProvider) {
     $httpProvider.interceptors.push(function($q) {
        return {
          responseError: function(rejection) {
                if(rejection.status <= 0) {
                    window.location = "noresponse.html";
                    return;
                }
                return $q.reject(rejection);
            }
        };
    });
});


来源:https://stackoverflow.com/questions/23914893/angular-js-http-intercept-neterr-connection-refused

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