AngularJS - 从附加请求拦截器中排除特定的AJAX请求?

时间:2017-02-17 00:12:27

标签: angularjs

我的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请求。

如何排除拦截器附加标题的特定请求?

1 个答案:

答案 0 :(得分:4)

简单选项...在$http配置中添加一个标志,告诉您的拦截器忽略它,例如

request: function(config) {
  if (!config.skipInterceptor && $rootScope.myToken) {
    // and so on
  }
  return config;
}

$http.get('http://some.external.api', {
  skipInterceptor: true
})
相关问题