Matteo Murgida
Matteo Murgida

Reputation: 161

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

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

Upvotes: 3

Views: 2446

Answers (2)

Shaun Lecathelinais
Shaun Lecathelinais

Reputation: 146

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.

Upvotes: 2

Ace.Yin
Ace.Yin

Reputation: 917

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 :)

Upvotes: 1

Related Questions