php获取ajax headers,无法从Ajax getAllResponseHeaders获得自定义HTTP标头响应

我确实获得了响应数据,但是无法获得自定义HTTP标头数据。

是的,这是一个跨域请求。我正在用Javascript执行Ajax请求。我已经尝试过XMLHttpRequest和jQuery $

.ajax。我已经完成了服务器设置,在发送数据时已进行了以下设置:

Access-Control-Allow-Origin: *

Access-Control-Allow-Methods: POST, GET

我 确实 得到了想要的响应数据。但是我无法获得完整的HTTP标头响应。

使用PHP,我在发送文本响应之前设置了以下内容。因此,我假设我应该使用getAllResponseHeaders()获得它。

header('Expires: 0');

header('Cache-Control: must-revalidate, post-check=0, pre-check=0');

header('My-Own-Test: nodatahere');

但是,这就是我得到的。

Content-Type: text/plain; charset=x-user-defined

Cache-Control: must-revalidate, post-check=0, pre-check=0

Expires: 0

它缺少My-Own-Test。仅供参考,这是我的Javascript:

var formData = new FormData();

formData.append('username', 'my_username');

formData.append('book_id', 'test password');

var xhr = new XMLHttpRequest();

xhr.open('POST', 'https://mydomain.com/proc.php', true);

xhr.overrideMimeType("text/plain; charset=x-user-defined");

xhr.onload = function(e) {

console.log(this.getAllResponseHeaders());

};

xhr.send(formData);

我什至用jQuery尝试过…同样的结果。

var data_to_send = {

username: 'my_username',

password: 'test_password'

};

var ajaxObj;

ajaxObj = $.ajax({

url: "https://mydomain.com/proc.php",

data: data_to_send,

type: "POST",

beforeSend: function ( xhr ) {

xhr.overrideMimeType("text/plain; charset=x-user-defined");

}

})

.done(function ( data ) {

console.log( ajaxObj.getAllResponseHeaders() );

});

仍然…没有运气。

但是,如果我去通过萤火虫或Chrome的开发者工具,我可以看到,这些工具做回完整的HTTP头信息,包括Content-Length,Content-

Encoding,Vary,X-Powered-By,Set-Cookie,Server,当然My-Own-Test。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值