在jQuery AJAX GET调用中传递请求标头。
在jQuery AJAX GET调用中传递请求标头。
我正在尝试使用jQuery在AJAX GET中传递请求头。在以下块中,“data”自动传递查询字符串中的值。是否有一种方法可以将该数据代替地传递到请求头中?
$.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); } });
admin 更改状态以发布 2023年5月23日
使用 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
从jQuery 1.5开始,可以通过以下方式传递一个headers
哈希表:
$.ajax({ url: "/test", headers: {"X-Test-Header": "test-value"} });
来自http://api.jquery.com/jQuery.ajax:
headers(自1.5版本起添加):与请求一起发送的其他头部键/值对的哈希表。在调用beforeSend函数之前设置此设置;因此,可以从beforeSend函数内部重写headers设置中的任何值。