Set defaults header on AngularJS but don't use it on one specific request

后端 未结 4 1224
半阙折子戏
半阙折子戏 2020-12-10 01:26

For sending OAuth2 token I am setting up defaults header on AngularJS like this:

$http.defaults.headers.common[\'Authorization\'] = \'Bearer \' + access_toke         


        
4条回答
  •  再見小時候
    2020-12-10 01:44

    For latecomers, whilst the solution might have worked - you actually shouldn't need to use transformRequest for this.

    The Angular docs for the $http service actually have this exact situation covered:

    To explicitly remove a header automatically added via $httpProvider.defaults.headers on a per request basis, Use the headers property, setting the desired header to undefined. For example:

     var req = {  
          method: 'POST',  
          url: 'http://example.com',  
          headers: {  
               'Content-Type': undefined  
          },  
          data: { 
               test: 'test' 
          } 
     }
    
     $http(req).success(function(){...}).error(function(){...});
    

提交回复
热议问题