简体   繁体   中英

Delete headers from Angular.js $http request

I want to delete some $http request header fields from one specific request (it means not on the $httpProvider level). These fields are:

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

How to do this in a single request? I tried to use transformRequest parameter, but didn't find enough information to make it work. Such a [CoffeeScript] code:

$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 says to me absolutely nothing), and I didn't understand what to return from the transformRequest function.

  1. If you use the unminified version of Angular, you'll get nicer backtraces when an exception happens, and you'll have an easier time introspecting the angular code. I personally recommend it while developing. Here's what headersGetter actually looks like:

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

    The data argument to your transformer will be undefined unless you're POSTing some data.

  2. The headersGetter function takes an optional argument name , if you want to get a single header, but you omit the argument to set a header:

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

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

  3. You can't change the Referer header from XHR.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM