javascript - 拦截器不工作

标签 javascript angularjs interceptor angular-http-interceptors

我正在尝试在 AngularJS 中创建一个拦截器。我是 AngularJS 的新手,找到了一些拦截器示例,但无法让它工作。

这里有我的 app.js 文件,其中包含所有相关代码。我还有一个调用 REST api 并返回 JSONP 的 Controller 。

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

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

最佳答案

$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');    
}]);

参见 Docs了解更多信息

关于javascript - 拦截器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21269007/

相关文章:

javascript - AngularJS、Browserify 和模块加载

c# - 如何使用断路器?

java - 我们可以在同一阶段拥有多个 CXF 拦截器吗

javascript - 如何使用 vue-chartjs 将图像添加到图表标签?

javascript - 通过 JS 发送时,PHP 图片上传不调整大小

javascript - 查找层次结构中最长链的长度

javascript - 尝试使用 PHP/Laravel 实现 Disqus SSO 时,无法设置未定义的属性 'remote_auth_s3'

angularjs - 如何在数据加载时在 AngularJS 中显示等待消息?

java - JAX RS 客户端 API 拦截器

javascript - 比较两个数组的 Angular 并对其进行循环