php ajax添加自定义头后报错,如何使用js或jQuery向ajax请求添加自定义HTTP标头?

9 个答案:

答案 0 :(得分:552)

根据您的需要,有几种解决方案......

如果您想将自定义标头(或标头集)添加到单个请求,则只需添加headers属性:

// Request with custom header

$.ajax({

url: 'foo/bar',

headers: { 'x-my-custom-header': 'some value' }

});

如果您想为每个请求添加默认标头(或标头集),请使用$.ajaxSetup():

$.ajaxSetup({

headers: { 'x-my-custom-header': 'some value' }

});

// Sends your custom header

$.ajax({ url: 'foo/bar' });

// Overwrites the default header with a new header

$.ajax({ url: 'foo/bar', headers: { 'x-some-other-header': 'some value' } });

如果您想为每个请求添加标题(或标题集),请使用beforeSend与$.ajaxSetup()挂钩:

$.ajaxSetup({

beforeSend: function(xhr) {

xhr.setRequestHeader('x-my-custom-header', 'some value');

}

});

// Sends your custom header

$.ajax({ url: 'foo/bar' });

// Sends both custom headers

$.ajax({ url: 'foo/bar', headers: { 'x-some-other-header': 'some value' } });

修改(更多信息):有一点需要注意的是,使用ajaxSetup时,您只能定义一组默认标头,而您只能定义一个beforeSend 。如果多次调用ajaxSetup,则只会发送最后一组标题,并且只会执行最后一次发送前回调。

答案 1 :(得分:51)

或者,如果您想为每个将来的请求发送自定义标头,那么您可以使用以下内容:

$.ajaxSetup({

headers: { "CustomHeader": "myValue" }

});

这样每个未来的ajax请求都将包含自定义标头,除非被请求的选项明确覆盖。您可以在ajaxSetup here上找到更多信息

答案 2 :(得分:18)

以下是使用XHR2的示例:

function xhrToSend(){

// Attempt to creat the XHR2 object

var xhr;

try{

xhr = new XMLHttpRequest();

}catch (e){

try{

xhr = new XDomainRequest();

} catch (e){

try{

xhr = new ActiveXObject('Msxml2.XMLHTTP');

}catch (e){

try{

xhr = new ActiveXObject('Microsoft.XMLHTTP');

}catch (e){

statusField('\nYour browser is not' +

' compatible with XHR2');

}

}

}

}

xhr.open('POST', 'startStopResume.aspx', true);

xhr.setRequestHeader("chunk", numberOfBLObsSent + 1);

xhr.onreadystatechange = function (e) {

if (xhr.readyState == 4 && xhr.status == 200) {

receivedChunks++;

}

};

xhr.send(chunk);

numberOfBLObsSent++;

};

希望有所帮助。

如果您创建对象,则可以使用setRequestHeader函数在发送请求之前指定名称和值。

答案 3 :(得分:17)

假设使用JQuery ajax,您可以添加自定义标头,如 -

$.ajax({

url: url,

beforeSend: function(xhr) {

xhr.setRequestHeader("custom_header", "value");

},

success: function(data) {

}

});

答案 4 :(得分:17)

您也可以在不使用jQuery的情况下执行此操作。覆盖XMLHttpRequest的send方法并在其中添加标题:

XMLHttpRequest.prototype.realSend = XMLHttpRequest.prototype.send;

var newSend = function(vData) {

this.setRequestHeader('x-my-custom-header', 'some value');

this.realSend(vData);

};

XMLHttpRequest.prototype.send = newSend;

答案 5 :(得分:12)

您应该避免使用docs中描述的$.ajaxSetup()。请改用以下内容:

$(document).ajaxSend(function(event, jqXHR, ajaxOptions) {

jqXHR.setRequestHeader('my-custom-header', 'my-value');

});

答案 6 :(得分:6)

假设您的意思是“当使用ajax”和“一个HTTP 请求标头”时,请使用您传递给headers的对象中的ajax()属性

标题(添加1.5)

默认值:{}

要与请求一起发送的其他标头键/值对的映射。在调用beforeSend函数之前设置此设置;因此,可以从beforeSend函数中覆盖标题设置中的任何值。

答案 7 :(得分:3)

应该使用XMLHttpRequest对象的

答案 8 :(得分:0)

您可以使用js fetch

async function send(url,data) {

let r= await fetch(url, {

method: "POST",

headers: {

"My-header": "abc"

},

body: JSON.stringify(data),

})

return await r.json()

}

// Example usage

let url='https://server.test-cors.org/server?enable=true&status=200&methods=POST&headers=my-header';

async function run()

{

let jsonObj = await send(url,{ some: 'testdata' });

console.log(jsonObj[0].request.httpMethod + ' was send - open chrome console > network to see it');

}

run();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值