我正在尝试阅读Ext.ajax.request即将发布的回复标题。
这是代码:
Ext.Ajax.request({ url: 'http://localhost:3000/v0.1/login' ,
method: 'POST',
scope:this,
jsonData: {"_username":username,"_userpwd":password},
success: function(responseObject){
var headers = responseObject.getAllResponseHeaders();
console.info(headers );
Ext.destroy(Ext.ComponentQuery.query('#loginWindow'));
this.application.getController('SiteViewController').showView();
},
failure: function(responseObject){
alert(responseObject.status);
}
});
但是在控制台中打印出的唯一标题是:
Object {content-type: "application/json; charset=utf-8"}
缺少所有其他标题,但它们出现在chrome检查器中!!!
我错过了什么?感谢
答案 0 :(得分:2)
由于您可能正在执行跨域请求,因此您只能使用服务器显式公开的标头。相同的域请求会公开所有标头。
在服务器端,您必须添加标题" Access-Control-Expose-Headers"带有您想要公开的标题的详尽列表,以逗号分隔。在php中它看起来像这样:
header("Access-Control-Expose-Headers: Content-length, X-My-Own-Header");
标题确实可以通过responseObject.getAllResponseHeaders()
或类似responseObject.getResponseHeader('content-type')
的内容获得。
有关跨域请求和标头的详细信息:http://www.html5rocks.com/en/tutorials/cors/
PS:Ace.Yin有正确的答案,但我没有足够的声誉来简单评论。答案 1 :(得分:0)
我遇到了同样的问题,最后我在这里找到了解决方案:http://www.html5rocks.com/en/tutorials/cors/
这是关于标题的部分:
Access-Control-Expose-Headers (optional) -
The XMLHttpRequest 2 object has a getResponseHeader() method that returns the value of
a particular response header. During a CORS request, the getResponseHeader() method
can only access simple response headers.
Simple response headers are defined as follows:
Cache-Control
Content-Language
Content-Type
Expires
Last-Modified
Pragma
If you want clients to be able to access other headers, you have to use the
Access-Control-Expose-Headers header. The value of this header is a comma-delimited
list of response headers you want to expose to the client.
我还没有验证它,但似乎在正确的轨道上:))