ExtJS 4.2.1: Cannot retrieve HTTP Response headers upon Ext.ajax.request callback

蓝咒 提交于 2020-01-14 22:52:05

问题


I'm trying to read the headers of the coming response upon Ext.ajax.request.

Here it is the code:

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

All the other headers are missing, but they are present in the chrome inspector!!!

What am I missing? Thanks


回答1:


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.

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");

The headers will indeed be available through responseObject.getAllResponseHeaders() or something like responseObject.getResponseHeader('content-type').

More information about cross-domain requests and headers: http://www.html5rocks.com/en/tutorials/cors/

PS: Ace.Yin had the right answer, but I don't have enough reputation to simply comment.




回答2:


i ran into the same issue and finally i found the solution here: http://www.html5rocks.com/en/tutorials/cors/

here is the part about the headers:

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.

i have not verify it yet, but it seems on the right track :)



来源:https://stackoverflow.com/questions/17066550/extjs-4-2-1-cannot-retrieve-http-response-headers-upon-ext-ajax-request-callbac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!