【问题标题】:Pass request headers in a jQuery AJAX GET call在 jQuery AJAX GET 调用中传递请求标头
【发布时间】:2011-03-16 13:51:37
【问题描述】:

我正在尝试使用 jQuery 在 AJAX GET 中传递请求标头。在下面的块中,“数据”自动传递查询字符串中的值。有没有办法在请求标头中传递该数据?

$.ajax({
         url: "http://localhost/PlatformPortal/Buyers/Account/SignIn",
         data: { signature: authHeader },
         type: "GET",
         success: function() { alert('Success!' + authHeader); }
      });

以下也没有用

$.ajax({
         url: "http://localhost/PlatformPortal/Buyers/Account/SignIn",
         beforeSend: { signature: authHeader },
         async: false,                    
         type: "GET",
                    success: function() { alert('Success!' + authHeader); }
      });

【问题讨论】:

    标签: jquery ajax client-side


    【解决方案1】:

    从 jQuery 1.5 开始,有一个 headers 散列可以传入如下:

    $.ajax({
        url: "/test",
        headers: {"X-Test-Header": "test-value"}
    });
    

    来自http://api.jquery.com/jQuery.ajax

    headers(添加 1.5):与请求一起发送的附加标头键/值对的映射。此设置在调用 beforeSend 函数之前设置;因此,headers 设置中的任何值都可以在 beforeSend 函数中被覆盖。

    【讨论】:

    • 是的:$.ajaxSetup({headers: {"X-Test-Header": "test-value"}})
    • jQuery 文档不再推荐使用 $.ajaxSetup() (api.jquery.com/jQuery.ajaxSetup)
    • @Glen 他们的理由是插件可能期望默认设置起作用。就个人而言,我认为如果您全局更改某些内容,则取决于默认设置的某些内容可能无法正常工作。
    • @Trip 我对全局的建议...为 ajax 调用(抽象)编写自己的包装器,而不是调用 $.ajax()。
    • @Rishav JSON 对象不允许出现在标头中。您必须对其进行 JSON.stringify 处理,然后在服务器上对其进行解析。
    【解决方案2】:

    使用beforeSend:

    $.ajax({
             url: "http://localhost/PlatformPortal/Buyers/Account/SignIn",
             data: { signature: authHeader },
             type: "GET",
             beforeSend: function(xhr){xhr.setRequestHeader('X-Test-Header', 'test-value');},
             success: function() { alert('Success!' + authHeader); }
          });
    

    http://api.jquery.com/jQuery.ajax/

    http://www.w3.org/TR/XMLHttpRequest/#the-setrequestheader-method

    【讨论】:

    • 我知道这已经过时了。但我想补充一点,后面应该有一个逗号: beforeSend: function(xhr){xhr.setRequestHeader('X-Test-Header', 'test-value');}
    • beforeSend: function(xhr){xhr.setRequestHeader('X-Test-Header', 'test-value');} 在 chrome 和 firefox 中运行良好,但在 IE8 中运行良好。没有 X-Test-Header 发送。如何解决?谢谢
    • 我已经尝试过,但是当我使用“jquery-1.11.1.min.js”传递标题详细信息时它会抛出错误
    • 见下面的答案,更相关
    • 如果我想将X-Test-HeaderX-Test-Header 添加到beforeSend 怎么办?
    【解决方案3】:

    $.ajax({
                url: URL,
                type: 'GET',
                dataType: 'json',
                headers: {
                    'header1': 'value1',
                    'header2': 'value2'
                },
                contentType: 'application/json; charset=utf-8',
                success: function (result) {
                   // CallBack(result);
                },
                error: function (error) {
                    
                }
            });

    【讨论】:

    • 使用 jQuery 1.7.2、C# API 2.x 尝试从标头HttpRequestMessage r = new HttpRequestMessage(); int mylogonID = Convert.ToInt32(r.Headers.GetValues("logonID")); 中提取时出错,因为 找不到给定的标头。 因为 r.Headers是空的。
    • 你可能想尝试类似 - string[] ids = System.Web.HttpContext.Current.Request.Headers["logonID"].Split(',');
    • dataType 应该写成广告数据类型
    猜你喜欢
    • 2015-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-07
    • 1970-01-01
    相关资源
    最近更新 更多