2013-06-12 32 views
2

我试图在Ext.ajax.request上读取即将到来的响应标头。ExtJS 4.2.1:无法在Ext.ajax.request回调时检索HTTP响应标头

这是代码:

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"} 

所有其他的头不见了,但它们存在于铬检查员!

我错过了什么?由于

回答

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. 

我还没有核实过,但它似乎对正确的轨道:)

1

因为你可能正在做一个跨域请求,你将只有服务器明确公开的头。相同的域请求公开所有标题。

在服务器端,您必须添加标题“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有正确的答案,但我没有足够的声誉简单地评论。