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

前端 未结 1 1601
粉色の甜心
粉色の甜心 2020-12-29 03:31

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 place

1条回答
  •  别那么骄傲
    2020-12-29 03:38

    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);
                }
            };
        });
    });
    

    0 讨论(0)
提交回复
热议问题