Setting angularjs $http headers globally

前端 未结 1 1442
抹茶落季
抹茶落季 2021-01-11 19:11

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.default

相关标签:
1条回答
  • 2021-01-11 19:53

    You should use one of two methods:

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

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

    Use interceptor

    var interceptor = function() {
      return {
        'request': function(config) {
          config.headers['Authorization'] = 'Basic Token';
         }
      }
    };
    
    angular.module('app', [])
      .config(function ($httpProvider) {
        $httpProvider.interceptors.push(interceptor);
    });
    
    0 讨论(0)
提交回复
热议问题