2017-02-17 81 views
1

我在我的角度定义的应用程序的请求拦截器,看起来像:AngularJS - 将请求拦截器附加到它的特定AJAX请求排除?

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'); 
}); 

上面附加一个API令牌来我所有的AJAX请求,并允许我,我的角应用程序,我的后端端点之间进行身份验证。

每隔一段时间,我都会向不支持custom-token标头的第三方API端点发送一个AJAX请求。

如何通过拦截器将头部附加到头部,从而排除该特定请求?

回答

2

容易的选择...添加标记到$http配置,它告诉你的拦截器忽略它,比如

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

$http.get('http://some.external.api', { 
    skipInterceptor: true 
}) 
+0

伟大的答案。我学到了东西。我可能会做一个域名数组,并检查请求域是否与'indexOf()'在数组中。这比这个想法更聪明。 – r3wt

+0

@ r3wt会建议作为另一种选择,但它会成为一个维护痛点 – Phil

+0

如果我错误地为我回答一个问题,这是否很酷?我用angular写了一个小型实用程序库,我写的同步迭代函数总是跳过数组中的第一个元素,我找不到原因。 – r3wt