3

我试图在 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 检查器中!!!

我错过了什么?谢谢

4

2 回答 2

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 有正确的答案,但我没有足够的声誉来简单评论。

于 2014-08-28T09:08:12.163 回答
1

我遇到了同样的问题,最后我在这里找到了解决方案: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.

我还没有验证它,但它似乎在正确的轨道上:)

于 2014-08-10T09:40:22.603 回答