Angular.js $ http拦截“net :: ERR_CONNECTION_REFUSED” [英] Angular.js $http intercept "net::ERR_CONNECTION_REFUSED"

查看:697
本文介绍了Angular.js $ http拦截“net :: ERR_CONNECTION_REFUSED”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 $ http'的拦截器为我的网站编写一个通用的错误处理程序,但它们似乎无法做我想要的执行。

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.

我将拦截器置于'响应''responseError'但是当服务器脱机/没有响应时(net :: ERR_CONNECTION_REFUSED),它们永远不会被调用。我理解为什么会发生这种情况,对拦截没有反应。

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.

我想知道是否有一种捕捉这些错误的通用方法,除了听取 $ httpPromise 's 错误每个请求的回调。

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.

推荐答案

您可以检查ResponseError的状态。当API离线时为0(直到Angular 1.3.18)或-1(自Angular 1.3.19起):

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

这篇关于Angular.js $ http拦截“net :: ERR_CONNECTION_REFUSED”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆