ajax jq 图片上传请求头,传递请求头中jQuery的AJAX GET调用

这篇博客讨论了如何在jQuery的AJAX GET请求中将数据传递到请求头而不是作为查询字符串。作者指出,`data`参数默认将值附加到URL,而通过`beforeSend`回调可以设置自定义请求头。示例代码展示了如何在`beforeSend`中使用`setRequestHeader`方法来添加自定义头部。
摘要由CSDN通过智能技术生成

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); }

});

解决方案

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); }

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值