3

I'm making a post request with ajax (CORS) and I am setting a header (Content-Type:application/x-www-form-urlencoded) and I'm trying to read the response's headers. Here is what I've done:

function makePostRequest(url, data, headers, httpVerb, dataType, elementId) {
    $.ajax({
        url: url,
        type: httpVerb,
        data: data,
        headers: headers,
        dataType: dataType,
        success: function(data, textStatus, jqXHR) {
            $("#" + elementId).val(jqXHR.responseText);
            alert(JSON.stringify(jqXHR));
        },
        error: function(jqXHR, textStatus, errorThrown) {
            $("#" + elementId).val(jqXHR.responseText);
        }
    }).then(function(data, status, xhr) {
        console.log(xhr.getAllResponseHeaders());
    });
}

But in the console is printed only

Content-Type: application/x-www-form-urlencoded; charset=utf-8

And in chrome developer tools I'm seeing: enter image description here

How to get all these headers?

PS: I am using Chrome, not Firefox ()

I asked how to get all headers, not why I'm getting only one header(if it's not possible, I will accept this answer).

Buda Gavril
  • 21,409
  • 40
  • 127
  • 196
  • 1
    A Google search for `ajax get all headers` seems to yield good results? – Pekka Jan 18 '17 at 15:32
  • 1
    Possible duplicate of [jqXHR.getAllResponseHeaders() won't return all headers](http://stackoverflow.com/questions/5614735/jqxhr-getallresponseheaders-wont-return-all-headers) – Pekka Jan 18 '17 at 15:32

1 Answers1

5

So, I was making a CORS request and in this case the headers are filtered out for security reasons.

The only way to acces those headers is to include in the response a header Access-Control-Expose-Headers that will contain a list of headers that can be read from the javascript, as you can read here:

7.1.1 Handling a Response to a Cross-Origin Request

User agents must filter out all response headers other than those that are a simple response header or of which the field name is an ASCII case-insensitive match for one of the values of the Access-Control-Expose-Headers headers (if any), before exposing response headers to APIs defined in CORS API specifications.

Buda Gavril
  • 21,409
  • 40
  • 127
  • 196