Javascript 角度:配置块中未注入http侦听器
我已经创建了一个简单的http拦截器,我打算用它记录请求和响应。问题是它在配置块中未定义,首先执行配置块并抛出错误,因为该模块无法实例化。以下是使用的代码:Javascript 角度:配置块中未注入http侦听器,javascript,angularjs,http,Javascript,Angularjs,Http,我已经创建了一个简单的http拦截器,我打算用它记录请求和响应。问题是它在配置块中未定义,首先执行配置块并抛出错误,因为该模块无法实例化。以下是使用的代码: authApp .factory("httpInterceptor",function($log){ var httpInterceptor = {}; httpInterceptor.request = function(config){ $log.info("request is being made
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);
});
提前谢谢 您的代码似乎是正确的,但有一行代码将您的全新拦截器推到
$httpProvider
拦截器列表中
$httpProvider.interceptors.push(httpInterceptor);
这一行不知道httpInterceptor是什么,因为它被定义为一个角度工厂。换成
$httpProvider.interceptors.push('httpInterceptor'); // Note the quotes around it.
然后重试。谢谢……整个流程现在也有意义了!!