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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15042439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 23:16:06  来源:igfitidea点击:

Can't Get Custom HTTP Header Response from Ajax getAllResponseHeaders

javascriptjqueryajaxcross-domain

提问by Iszuddin Ismail

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. I've tried this with XMLHttpRequest and also jQuery $.ajax. I've done my server settings, I have these set when sending data:

是的,这是一个跨域请求。我正在使用 Javascript 执行 Ajax 请求。我已经用 XMLHttpRequest 和 jQuery $.ajax 试过了。我已经完成了我的服务器设置,我在发送数据时设置了这些:

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

I doget 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. So I assume that I should get it with getAllResponseHeaders().

使用 PHP,我在发送文本响应之前设置了以下内容。所以我假设我应该使用 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. Just for reference sake, here's my Javascript:

它缺少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);

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-Length, Content-Encoding, Vary, X-Powered-By, Set-Cookie, Server, 当然还有My-Own-Test.

回答by Iszuddin Ismail

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

我要感谢jbl为我指出正确的 SO 问题。我现在明白了...

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. But use your own language, what ever you use to set them.

上面的例子使用 PHP。但是使用您自己的语言,无论您使用什么来设置它们。

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.

当我问这个问题时,除了Access-Control-Expose-Headers. 把它放进去后,我的 Javascript Ajax 可以读取 HTTP Header Custom-Header 的内容。