问题
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:
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).
回答1:
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.
来源:https://stackoverflow.com/questions/41723138/ajax-response-cannot-read-all-the-headers-from-the-response