ExtJS 4.2.1:无法在 Ext.ajax.request 回调时检索 HTTP 响应标头 [英] ExtJS 4.2.1: Cannot retrieve HTTP Response headers upon Ext.ajax.request callback
问题描述
我正在尝试读取 Ext.ajax.request 上即将到来的响应的标头.
I'm trying to read the headers of the coming response upon 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);
}
});
但它在控制台中打印出来的唯一标题是:
But the only header that it is printed out in console is:
Object {content-type: "application/json; charset=utf-8"}
所有其他标题都丢失了,但它们存在于 chrome 检查器中!!!
All the other headers are missing, but they are present in the chrome inspector!!!
我错过了什么?谢谢
推荐答案
因为您可能正在执行跨域请求,所以您只会有服务器显式公开的标头.相同的域请求会公开所有标头.
Because you're probably doing a cross-domain request, you will only have headers explicitly exposed by the server. Same domain requests expose all the headers.
在服务器端,您必须添加标题Access-Control-Expose-Headers",其中包含要公开的标题的详尽列表,以逗号分隔.在 php 中,它看起来像这样:
On the server side you have to add the header "Access-Control-Expose-Headers" with the exhaustive list of headers you want to expose, separated by a coma. In php it would look like this:
header("Access-Control-Expose-Headers: Content-length, X-My-Own-Header");
标题确实可以通过 responseObject.getAllResponseHeaders()
或类似 responseObject.getResponseHeader('content-type')
的东西获得.
The headers will indeed be available through responseObject.getAllResponseHeaders()
or something like responseObject.getResponseHeader('content-type')
.
有关跨域请求和标头的更多信息:http://www.html5rocks.com/en/tutorials/cors/
More information about cross-domain requests and headers: http://www.html5rocks.com/en/tutorials/cors/
PS:Ace.Yin 的答案是正确的,但我没有足够的声誉来简单地评论.
PS: Ace.Yin had the right answer, but I don't have enough reputation to simply comment.
这篇关于ExtJS 4.2.1:无法在 Ext.ajax.request 回调时检索 HTTP 响应标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!