24

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);
Kirby
  • 15,127
  • 10
  • 89
  • 104
Eddy Navarro
  • 241
  • 1
  • 2
  • 4
  • Hi Eddy. Can you post some code? – Adam Hopkinson Apr 10 '11 at 21:21
  • @adam: Sure! I just added an example. – Eddy Navarro Apr 10 '11 at 21:43
  • 1
    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 Apr 10 '11 at 21:57
  • 1
    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 Apr 10 '11 at 22:02
  • @no.good.at.coding: I am making cross-domain request. – Eddy Navarro Apr 10 '11 at 22:13
  • 1
    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 Apr 10 '11 at 23:11
  • 2
    It also appears that even with JSONP, you will not get the response headers, only the data: http://forum.jquery.com/topic/get-response-headers-cross-domain – no.good.at.coding Apr 10 '11 at 23:46
  • 3
    This has been answered here: http://stackoverflow.com/questions/14686769/xmlhttp-getresponseheader-not-working-for-cors – svenyonson Apr 07 '14 at 21:34
  • This is the best answer http://stackoverflow.com/questions/1557602/jquery-and-ajax-response-header – Ivor Scott Mar 10 '17 at 10:43

2 Answers2

15

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.

Chris
  • 6,805
  • 3
  • 35
  • 50
  • Documentation: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Expose-Headers – bmatovu Nov 15 '18 at 10:38
7

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/

Fabio Buda
  • 769
  • 2
  • 7
  • 16