简体   繁体   English

无法从Ajax getAllResponseHeaders获取自定义HTTP标头响应

[英]Can't Get Custom HTTP Header Response from Ajax getAllResponseHeaders

I do get the response data, but I can't get my custom HTTP header data. 我确实得到了响应数据,但我无法获取自定义HTTP标头数据。

Yes, this is a cross-domain request. 是的,这是一个跨域请求。 I am doing an Ajax request with Javascript. 我正在用Javascript做一个Ajax请求。 I've tried this with XMLHttpRequest and also jQuery $.ajax. 我用XMLHttpRequest和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

I do get the response data that I want. 确实得到了我想要的响应数据。 But I can't get full HTTP header response. 但我无法获得完整的HTTP标头响应。

With PHP, I set the following before sending the text response. 使用PHP,我在发送文本响应之前设置了以下内容。 So I assume that I should get it with getAllResponseHeaders(). 所以我假设我应该使用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

It's missing the My-Own-Test . 它错过了My-Own-Test Just for reference sake, here's my Javascript: 仅供参考,这是我的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);

I even tried it with jQuery... same result. 我甚至尝试用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()  );
});

Still... no luck. 还是......没有运气。

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 . 但是,如果我浏览Firebug或Chrome的开发人员工具,我可以看到这些工具确实返回完整的HTTP标头信息,包括Content-LengthContent-EncodingVaryX-Powered-BySet-CookieServer ,当然My-Own-Test

I wanna thank jbl for pointing me to the right SO question. 我想感谢jbl指出我正确的SO问题。 I got it now... 我现在明白了......

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. 如果你想设置自己的HTTP头信息,然后使用跨域Ajax或类似的东西来获取它,那么在发送响应文本之前,你应该在服务器端设置一些额外的HTTP头。

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

Example above uses PHP. 上面的例子使用PHP。 But use your own language, what ever you use to set them. 但是使用你自己的语言,你用它来设置它们。

When I asked this question, I had all of that except Access-Control-Expose-Headers . 当我问这个问题时,除了Access-Control-Expose-Headers之外我还有其他所有问题。 After putting that in, my Javascript Ajax can read the content of HTTP Header Custom-Header. 把它放进去后,我的Javascript Ajax可以读取HTTP Header Custom-Header的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM