Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/432.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Angular.js$http intercept“;网络::错误“连接被拒绝”;_Javascript_Angularjs_Http_Interceptor - Fatal编程技术网

Javascript Angular.js$http intercept“;网络::错误“连接被拒绝”;

Javascript Angular.js$http intercept“;网络::错误“连接被拒绝”;,javascript,angularjs,http,interceptor,Javascript,Angularjs,Http,Interceptor,我正试图使用$http's拦截器为我的网站编写一个通用的错误处理程序,但它们似乎无法完成我想做的事情 我在'response'和'responseError'上放置了拦截器,但当服务器脱机/未重新发送时,它们从未被调用(net::ERR\u CONNECTION\u拒绝)。我理解为什么会发生这种情况,截获没有反应 我想知道除了监听每个请求的$httpPromise错误回调之外,是否有捕获这些错误的通用方法。您可能可以检查响应错误的状态。当API脱机时为0(直到Angular 1.3.18)或-1

我正试图使用
$http'
s拦截器为我的网站编写一个通用的错误处理程序,但它们似乎无法完成我想做的事情

我在
'response'
'responseError'
上放置了拦截器,但当服务器脱机/未重新发送时,它们从未被调用(net::ERR\u CONNECTION\u拒绝)。我理解为什么会发生这种情况,截获没有反应


我想知道除了监听每个请求的
$httpPromise
错误
回调之外,是否有捕获这些错误的通用方法。

您可能可以检查响应错误的状态。当API脱机时为0(直到Angular 1.3.18)或-1(从Angular 1.3.19开始):

angular.module(“services.interceptor”,arguments).config(函数($httpProvider){
$httpProvider.interceptors.push(函数($q){
返回{
响应者:功能(拒绝){

if(rejection.status Yes)似乎有效。我想我试着听了
responseError
,但很明显,有些事情我做得不对。有没有办法在“配置时间”之后处理此类错误或者当$scope可用时?为了使用控制器显示错误。显示网络错误可能是错误的方式,但我找不到正确的方式…是的,您可以注入
$injector
并转换到某个状态。例如:``angular.module('module').factory('accessHttpInterceptor',['$injector',function('injector'){return{responseError:function accessHttpInterceptorResponseError(拒绝){var$state=$injector.get('$state');$state.go('offline');}};};}]);“``这个答案对$q定义有帮助:
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);
            }
        };
    });
});