我怎样才能获得通过跨域Ajax响应头? [英] How can I get response header via cross-domain ajax?
问题描述
我想阅读的文档,我必须承认这不是一件容易的阅读。我有没有问题(添加访问控制 - 允许 - 产地标头后)读取的responseText,但未能得到响应头的任何地方,除了Firefox浏览器。
I'm trying to read documentation and I must confess it is not an easy reading. I have no problem (after adding Access-Control-Allow-Origin header) to read responseText, but fail to get response header anywhere except Firefox.
所以,我的问题是什么是正确的方式来获得响应头,使用跨域AJAX?
So, my question is what is the right way to get response header, using cross-domain ajax?
我试图用(<一href="https://developer.mozilla.org/en/http_access_control#Access-Control-Expose-Header">Access-Control-Expose-Headers),但同样,无法读取头。
I've tried to use (Access-Control-Expose-Headers), but, again, failed to read header.
推荐答案
所以它应该工作的方式是,你指定你希望客户端访问的标头中的访问控制,揭露-头头。例如,如果你的服务器设置一个Foo响应头,并且希望客户端能够阅读它,您的服务器也应该发送下面的标题:
So the way it should work is that you specify the headers you want the client to have access to in the Access-Control-Expose-Headers header. For example, if your server sets a Foo response header, and you want the client to be able to read it, your server should also send the following header:
Access-Control-Expose-Headers: Foo
在客户端,您可以通过调用xhr.getAllResponseHeaders读取所有的响应头()。这将返回的响应头为一个字符串,然后您可以解析到一个对象使用下列code:的https: //gist.github.com/706839
On the client side, you can read all the response headers by calling xhr.getAllResponseHeaders(). This returns the response headers as a string, which you can then parse into an object using the following code: https://gist.github.com/706839
这就是事情的的应该的工作进行说明。但是,注意,是在旧的浏览器了一个错误的响应头不能被读取客户机上。在这里看到更多的细节:<一href="http://stackoverflow.com/questions/4561221/cors-xmlhtt$p$pquest-head-method/4863887#486388">CORS xmlhtt prequest HEAD方法
That is an explanation of how things should work. However, note that there is a bug in older browsers where the response headers can't be read on the client. See here for more details: CORS xmlhttprequest HEAD method
这篇关于我怎样才能获得通过跨域Ajax响应头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!