从Angular.js $ http请求中删除标头

时间:2022-10-29 19:41:16

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

我想从一个特定的请求中删除一些$ http请求头字段(这意味着不在$ httpProvider级别)。这些字段是:

  • Cache-Control
  • 缓存控制
  • If-Modified-Since
  • 如果-Modified-Since的
  • Referer
  • 引荐
  • X-Requested-With
  • X-要求,随着

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:

如何在一个请求中执行此操作?我试图使用transformRequest参数,但没有找到足够的信息使其工作。这样的[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 says to me absolutely nothing), and I didn't understand what to return from the transformRequest function.

表明数据是未定义的,headersGetter是函数(c){a ||(a = Nb(b));返回c?a [y(c)] || null:a}(它绝对没有对我说),我不明白从transformRequest函数返回什么。

1 个解决方案

#1


7  

  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:

    如果你使用Angular的非限制版本,当异常发生时你会得到更好的回溯,你可以更容易地反省角度代码。我个人在开发时推荐它。这是headersGetter实际上的样子:

    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函数采用可选的参数名称,如果您想获得单个标头,但省略了设置标头的参数:

    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.

    您无法从XHR更改Referer标头。

#1


7  

  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:

    如果你使用Angular的非限制版本,当异常发生时你会得到更好的回溯,你可以更容易地反省角度代码。我个人在开发时推荐它。这是headersGetter实际上的样子:

    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函数采用可选的参数名称,如果您想获得单个标头,但省略了设置标头的参数:

    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.

    您无法从XHR更改Referer标头。