我的Angular应用程序中定义了一个请求拦截器,如下所示:
precinct.factory('httpRequestInterceptor', function($rootScope){
return {
request: function(config){
if($rootScope.myToken){
config.headers['custom-token'] = $rootScope.myToken;
}
return config;
}
}
});
precinct.config(function($httpProvider){
$httpProvider.interceptors.push('httpRequestInterceptor');
});
上面为我的所有AJAX请求附加了一个API令牌,并允许我在我的Angular应用程序和后端端点之间进行身份验证。
每隔一段时间,我会向不支持custom-token
标题的第三方API端点发送AJAX请求。
如何排除拦截器附加标题的特定请求?
答案 0 :(得分:4)
简单选项...在$http
配置中添加一个标志,告诉您的拦截器忽略它,例如
request: function(config) {
if (!config.skipInterceptor && $rootScope.myToken) {
// and so on
}
return config;
}
和
$http.get('http://some.external.api', {
skipInterceptor: true
})