在 jQuery AJAX GET 调用中传递请求头

I am trying to pass request headers in an AJAX GET using jQuery. In the following block, "data" automatically passes the values in the querystring. Is there a way to pass that data in the request header instead ?

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

The following didn't work either

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

转载于:https://stackoverflow.com/questions/3258645/pass-request-headers-in-a-jquery-ajax-get-call

3个回答

Use 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

csdnceshi50
三生石@ this response is the correct one if you use jsonp, but if you don't see @Lukas response below. Warning if you both use https for url AND jsonp it won't work.
2 年多之前 回复
csdnceshi55
~Onlooker is this possible with "jsonp"?
2 年多之前 回复
weixin_41568174
from.. What if I want to add X-Test-Header and X-Test-Header to the beforeSend?
3 年多之前 回复
csdnceshi74
7*4 see below answer, much more relevant
接近 4 年之前 回复
csdnceshi69
YaoRaoLov i have tried but it throw error when i am passing header detail in using "jquery-1.11.1.min.js"
4 年多之前 回复
csdnceshi54
hurriedly% beforeSend: function(xhr){xhr.setRequestHeader('X-Test-Header', 'test-value');} works well in chrome and firefox but not in IE8. there is no X-Test-Header send. how to fix it? Thx
5 年多之前 回复
csdnceshi61
derek5. i know this is super-old by now. but I wanted to add that there should be a comma after: beforeSend: function(xhr){xhr.setRequestHeader('X-Test-Header', 'test-value');}
7 年多之前 回复

As of jQuery 1.5, there is a headers hash you can pass in as follows:

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

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

headers (added 1.5): A map of additional header key/value pairs to send along with the request. This setting is set before the beforeSend function is called; therefore, any values in the headers setting can be overwritten from within the beforeSend function.

weixin_41568208
北城已荒凉 be aware this doesn't work with jsonp (see stackoverflow.com/questions/7433556/…) for jsonp refere to @Adam response
2 年多之前 回复
csdnceshi79
python小菜 My recommendation for globally... write your own wrapper for ajax calls (abstraction) instead of calling $.ajax().
大约 4 年之前 回复
weixin_41568174
from.. Their reasoning is that plugins might be expecting default settings to work. Personally, I think it's a given that if you globally change something, something that was depending on the default settings might not work.
接近 6 年之前 回复
csdnceshi70
笑故挽风 The jQuery docs don't recommend using $.ajaxSetup() anymore (api.jquery.com/jQuery.ajaxSetup)
大约 6 年之前 回复
csdnceshi51
旧行李 Yes: $.ajaxSetup({headers: {"X-Test-Header": "test-value"}})
7 年多之前 回复
csdnceshi54
hurriedly% Can this be set globally?
7 年多之前 回复

$.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) {
                
            }
        });

</div>
csdnceshi73
喵-见缝插针 you may want to try something like - string[] ids = System.Web.HttpContext.Current.Request.Headers["logonID"].Split(',');
3 年多之前 回复
csdnceshi76
斗士狗 Using jQuery 1.7.2, C# API 2.x, when trying to extract from header HttpRequestMessage r = new HttpRequestMessage(); int mylogonID = Convert.ToInt32(r.Headers.GetValues("logonID")); error out because The given header was not found. because r.Headers is empty.
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问