设置angularjs $ HTTP头全球全球、angularjs、HTTP

由网友(南馆潇湘)分享简介:我有多个控制器web应用程序。我在我的控制器之一(通过 http.defaults.headers.common ['设置 $ HTTP 服务的默认标题在回调headername'] )。然而,这些头没有得到来自其他控制器的后续调用设置。我必须将它们设置为每个控制器我还是不够一次?I have a webapp wi...

我有多个控制器web应用程序。我在我的控制器之一(通过 http.defaults.headers.common ['设置 $ HTTP 服务的默认标题在回调headername'] )。然而,这些头没有得到来自其他控制器的后续调用设置。我必须将它们设置为每个控制器我还是不够一次?

I have a webapp with multiple controllers. I'm setting the defaults headers of the $http service in a callback in one of my controllers (via http.defaults.headers.common['headername']). However, those headers do not get set in subsequent calls from other controllers. Do I have to set them for each controller I have or is one time enough?

推荐答案

您应该使用以下两种方法之一:

You should use one of two methods:

在运行段设置$ http.defaults.headers例如

Set $http.defaults.headers in run block e.g.

 module.run(function($http) {
  $http.defaults.headers.common.Authorization = 'Basic Token';
});

使用拦截器

var interceptor = function() {
  return {
    'request': function(config) {
      config.headers['Authorization'] = 'Basic Token';
     }
  }
};

angular.module('app', [])
  .config(function ($httpProvider) {
    $httpProvider.interceptors.push(interceptor);
});
阅读全文

相关推荐

最新文章