9

我试图在AngularJS中制作一个拦截器。我对AngularJS颇为陌生,发现了一些拦截器的例子,但无法使其工作。拦截器不工作

这里我有我的app.js文件,它有所有相关的代码。我也有一个控制器,它调用REST API并返回JSONP。

首先我声明模块,然后配置它(定义拦截器)。它现在应该捕获所有请求并输出到控制台...

使用app.factory创建拦截器是错误的吗?

var app = angular.module(
    'TVPremieresApp', 
    [ 
    'app.services' 
     , 'app.controllers' 
    ] 
); 

app.config(function ($httpProvider) { 
    $httpProvider.responseInterceptors.push('errorInterceptor'); 
}); 

app.service('MessageService', function() { 
    // angular strap alert directive supports multiple alerts. 
    // Usually this is a distraction to user. 
    //Let us limit the messages to one  
    this.messages = []; 
    this.setError = function(msg) { 
     this.setMessage(msg, 'error', 'Error:'); 
    }; 
    this.setSuccess = function(msg) { 
     this.setMessage(msg, 'success', 'Success:'); 
    }; 
    this.setInfo = function (msg) { 
     this.setMessage(msg, 'info', 'Info:'); 
    };  
    this.setMessage = function(content, type, title) { 
     var message = { 
      type: type, 
      title: title, 
      content: content 
     }; 
     this.messages[0] = message; 
    }; 
    this.clear = function() { 
     this.messages = []; 
    }; 
}); 

app.factory('errorInterceptor', function ($q, $location, MessageService, $rootScope) { 
    return function (promise) { 
    // clear previously set message 
    MessageService.clear(); 

    return promise.then(function (response) { 
     console.log(response); 
     return response; 
    }, 
    function (response) { 
     if (response.status == 404) { 
     MessageService.setError('Page not found'); 
     } 
     else if(response.status >= 500){ 
     var msg = "Unknown Error."; 

     if (response.data.message != undefined) { 
      msg = response.data.message + " "; 
     } 
     MessageService.setError(msg); 
     } 
     // and more 
     return $q.reject(response); 
    }); 
    }; 
}); 
+0

您正在使用的angularjs的版本见Docs? – Satpal

+0

responseInterceptors已被弃用。检查http://stackoverflow.com/a/20632690/1014586看看你如何适应你的代码 – mbarthelemy

回答

33

$httpProvider.responseInterceptors已弃用。您可以通过修改代码

app.factory('errorInterceptor', ['$q', '$rootScope', 'MessageService', '$location', 
    function ($q, $rootScope, MessageService, $location) { 
     return { 
      request: function (config) { 
       return config || $q.when(config); 
      }, 
      requestError: function(request){ 
       return $q.reject(request); 
      }, 
      response: function (response) { 
       return response || $q.when(response); 
      }, 
      responseError: function (response) { 
       if (response && response.status === 404) { 
       } 
       if (response && response.status >= 500) { 
       } 
       return $q.reject(response); 
      } 
     }; 
}]); 

app.config(['$httpProvider', function ($httpProvider) { 
    $httpProvider.interceptors.push('errorInterceptor');  
}]); 

更多信息

+0

@satpal在这个问题上的任何想法http://stackoverflow.com/questions/30590952/keyup-event-alternative?noredirect= 1#comment49252528_30590952 –

+0

感谢“responseError”拦截器字段示例。当我尝试实现一个jwt令牌过期时,我的代码在接收到401(未授权)时触及该块!我期待它在401上触发'response'模块,而不是在401上。 [*如果您感兴趣,请链接到我的代码*](https://github.com/blaskovicz/MagicStick/blob/master/frontend/ app.coffee#L150)。 – Blaskovicz