jqXHR.getAllResponseHeaders() won't return all headers

27,486

Solution 1

svenyonson called this out in the comments, but for me it was the answer, so I'm elevating it. If you're doing CORS, the server has to be explicit about what headers the client is allowed to read. If you want to read X-My-CustomHeader in javascript, then this header should be in the server response:

Access-Control-Expose-Headers: X-My-CustomHeader

More detail here.

Solution 2

From jquery official website:

At present, due to a bug in Firefox where .getAllResponseHeaders() returns the empty string although .getResponseHeader('Content-Type') returns a non-empty string, automatically decoding JSON CORS responses in Firefox with jQuery is not supported.

http://api.jquery.com/jQuery.ajax/

Share:
27,486

Related videos on Youtube

Eddy Navarro
Author by

Eddy Navarro

Updated on July 09, 2022

Comments

  • Eddy Navarro
    Eddy Navarro almost 2 years

    After a jQuery.ajax() call jqXHR.getAllResponseHeaders() won't return all the headers. The server responded with the following headers:

    Connection: keep-alive
    Content-Length: 64
    Content-Type: application/json
    X-My-CustomHeader: whatever
    

    getAllResponseHeaders() returned only:

    Content-Type: application/json
    

    What am I doing wrong?

    Example

    var request = {
      'url': 'http://api.someExternalDomain.com/resource/',
      'type': someMethod,
      'success': function(data, textStatus, jqXHR) {
        console.log(jqXHR.getAllResponseHeaders());
      }
    };
    
    $.ajax(request);
    
    • no.good.at.coding
      no.good.at.coding about 13 years
      This works for me on FF4 and Chrome. And I'm not entirely sure since you seem to be getting some data and your success callback function is triggered, but it might be the cross-domain request you're making that's the problem.
    • no.good.at.coding
      no.good.at.coding about 13 years
      Also, what browser are you seeing this problem on? And have you tried logging the headers you get from a non-cross-domain request.
    • no.good.at.coding
      no.good.at.coding about 13 years
      Hm, I wouldn't expect that to work properly - unless you're using JSONP which it doesn't look like you're doing. I still don't see how you're getting any response at all given that it's a cross domain request. What environment are you running this in? Perhaps the solution might be to get rid of the CDR.
    • no.good.at.coding
      no.good.at.coding about 13 years
      It also appears that even with JSONP, you will not get the response headers, only the data: forum.jquery.com/topic/get-response-headers-cross-domain
    • svenyonson
      svenyonson about 10 years
      This has been answered here: stackoverflow.com/questions/14686769/…
  • bmatovu
    bmatovu over 5 years

Related