【发布时间】:2015-09-16 10:52:03
【问题描述】:
我想拦截资源调用中的请求。我的拦截器的目的是拦截发送到服务器的所有请求的 http 请求和授权标头。这是我的拦截器:
module.exports = function (authToken) {
return {
request: function(config){
var token = authToken.getToken();
if(token){
config.headers.Authorization = 'Bearer ' + token;
}
console.log(config);
return config;
},
response: function(response){
return response;
}
}
};
作为配置函数的一部分,我正在推送 authInterceptor:
$httpProvider.interceptors.push('authInterceptor');
我还将拦截器定义为 $resource 参数:
module.exports = function ($resource, gwService, authInterceptor) {
return $resource(gwService.getResourceUrl('/environments/:environmentId'), null,
{
'update': {method: 'PUT'},
'query': {method: 'GET', interceptor: authInterceptor}
}
);
};
我无法弄清楚为什么 authInterceptor 永远不会被执行。是否可以以角度拦截 $resource 或者我必须使用 $http?
【问题讨论】:
-
你不需要在$resource中传递拦截器
-
还是一样 :) 问题是 Authorization 标头从未添加到 http 请求中
标签: angularjs interceptor angular-resource