我想攔截資源調用中的請求。我的攔截器的目的是攔截髮送到服務器的所有請求的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');
我也定義攔截器作爲$資源參數:
module.exports = function ($resource, gwService, authInterceptor) {
return $resource(gwService.getResourceUrl('/environments/:environmentId'), null,
{
'update': {method: 'PUT'},
'query': {method: 'GET', interceptor: authInterceptor}
}
);
};
我不能弄清楚爲什麼authInterceptor永遠不會執行。是有可能攔截$資源角度或我必須使用$ http?
的,你不需要通過在$資源攔截 – maddygoround
it's還是一樣:)問題是,授權頭從未添加到http請求 – Sanja