Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jqXHR.getAllResponseHeaders() won't return all headers

Tags:

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); 
like image 594
Eddy Navarro Avatar asked Apr 10 '11 21:04

Eddy Navarro


1 Answers

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.

like image 128
Chris Avatar answered Sep 27 '22 18:09

Chris