不能从阿贾克斯getAllResponseHeaders自定义HTTP头响应 [英] Can't Get Custom HTTP Header Response from Ajax getAllResponseHeaders

查看:468
本文介绍了不能从阿贾克斯getAllResponseHeaders自定义HTTP头响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到的响应数据,但我不能让我的自定义HTTP头数据。

I do get the response data, but I can't get my custom HTTP header data.

是的,这是一个跨域请求。我在做一个Ajax请求的Jav​​aScript。我一直试图与XMLHtt prequest也jQuery的$。阿贾克斯。我已经做了我的服务器的设置,我在发送数据时具有以下设置:

Yes, this is a cross-domain request. I am doing an Ajax request with Javascript. I've tried this with XMLHttpRequest and also jQuery $.ajax. I've done my server settings, I have these set when sending data:

Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET

我做得到我想要的响应数据。但我不能得到充分的HTTP标头的响应。

I do get the response data that I want. But I can't get full HTTP header response.

使用PHP,我设定发送文本响应之前以下。所以,我认为我应该得到它与getAllResponseHeaders()。

With PHP, I set the following before sending the text response. So I assume that I should get it with getAllResponseHeaders().

header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('My-Own-Test: nodatahere');

但这里是我得到了。

But here's what I got.

Content-Type: text/plain; charset=x-user-defined
Cache-Control: must-revalidate, post-check=0, pre-check=0
Expires: 0

它缺少我 - 拥有 - 测试。仅供参考缘故,这里是我的javascript:

It's missing the My-Own-Test. Just for reference sake, here's my 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 ...相同的结果。

I even tried it with jQuery... same result.

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

不过......没有运气。

Still... no luck.

但是,如果我通过萤火虫或Chrome的开发人员工具,我可以看到,这些工具做回完整的HTTP头信息,包括内容长度内容编码因人而异 X-Powered-By中设置Cookie 服务器,当然我 - 拥有 - 测试

But if I go through Firebug or Chrome's Developer Tool, I can see that those tools do return full HTTP header information, including Content-Length, Content-Encoding, Vary, X-Powered-By, Set-Cookie, Server, and of course My-Own-Test.

推荐答案

我要感谢 JBL 指着我的正确因此质疑。我现在...

I wanna thank jbl for pointing me to the right SO question. I got it now...

那么,OK ......答案。如果你想设置自己的HTTP头信息,然后利用跨域Ajax,或者类似的东西取水的时候,这里有一些额外的HTTP头,你应该设置你的服务器端,发送响应文本之前。

So, OK... the answer. If you ever wanted to set your own HTTP Header information, and then fetch it using cross-domain Ajax, or something like that, here are some extra HTTP Header you should set on your server side, before sending the response text.

header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Methods: POST, GET");      
header('Custom-Header: Own-Data');
header('Access-Control-Expose-Headers: Custom-Header');

以上示例使用PHP。但是,用你自己的语言,你什么都用它来进行设置。

Example above uses PHP. But use your own language, what ever you use to set them.

当我问到这个问题,我把所有的,除了访问控制 - 揭露-头。将在后,我的JavaScript的Ajax可以读取HTTP头定制头的内容。

When I asked this question, I had all of that except Access-Control-Expose-Headers. After putting that in, my Javascript Ajax can read the content of HTTP Header Custom-Header.

这篇关于不能从阿贾克斯getAllResponseHeaders自定义HTTP头响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆