Remove headers from Angular.js $ http request

I want to remove several header fields of the $http request from one specific request (this means that it is not at the level of $httpProvider ). These fields are:

  • Cache control
  • If-Modified-Since
  • Referer
  • X-Requested-With

How to do it in one request? I tried using the transformRequest parameter but did not find enough information to make it work. Such code [CoffeeScript]:

 $scope.logout = -> $http({ method: 'GET' url: '/api/logout' headers: { 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' } transformRequest: (data, headersGetter) -> console.log data console.log headersGetter data }).success -> $location.path('editor') 

shows that data is undefined , headersGetter is function (c){a||(a=Nb(b));return c?a[y(c)]||null:a} (which tells me absolutely nothing) , and I did not understand what to return from the transformRequest function.

+7
source share
1 answer
  • If you use an unlimited version of Angular, you will get nicer backtracks when an exception occurs, and it will be easier for you to view the angular code. I personally recommend it when developing. Here's what headersGetter looks headersGetter :

     function (name) { if (!headersObj) headersObj = parseHeaders(headers); if (name) { return headersObj[lowercase(name)] || null; } return headersObj; } 

    The data argument for your transformer will be undefined unless you send POST some data.

  • The headersGetter function takes an optional name argument if you want to get one header, but you omit the argument to set the header:

     headersGetter()['Cache-Control'] = 'no-cache'; headersGetter()['X-Requested-With'] = ''; 

    The return value of your transformer should be the data value that you want to use.

  • You cannot change the Referer header from XHR.

+7
source

All Articles