Angular:http拦截器没有被注入配置块

时间:2017-06-07 14:23:31

标签: javascript angularjs http

我创建了一个简单的http拦截器,我打算用它来记录请求和响应。问题是它在config块中是未定义的,它首先执行并抛出错误,因为哪个模块无法实例化。以下是使用的代码:

authApp
.factory("httpInterceptor",function($log){
    var httpInterceptor = {};

    httpInterceptor.request = function(config){
        $log.info("request is being made to " + config.url);
        return config;
    };

    httpInterceptor.response = function(config){
        $log.info("request to URL : " + config.url + " is completed 
        now.");
        return config;
    };

    return httpInterceptor;
})
.config(function($httpProvider){
    // Http Interceptor
    $httpProvider.interceptors.push(httpInterceptor);
 });

提前致谢!!

1 个答案:

答案 0 :(得分:1)

您的代码似乎是正确的,除了您将全新拦截器推送到$httpProvider拦截器列表的特定行。

$httpProvider.interceptors.push(httpInterceptor);

此行不知道httpInterceptor是什么,因为它被定义为角度工厂。将其更改为

$httpProvider.interceptors.push('httpInterceptor'); // Note the quotes around it.

并重试。