2

I am trying to read response headers using JQuery and Ajax but not all headers are returned. Only few of them.

$(document).ready(function () {

  $.ajax({
    type: 'POST',
    url:'http://do.convertapi.com/Word2Pdf',
    data: '',
    complete: function(resp){
        alert(resp.getAllResponseHeaders());
         }});

});

Working example: http://jsfiddle.net/tomasr/7jWSv/3/

The server response has CORS header included above, so it should be no problems accessing headers using ajax?

Access-Control-Allow-Origin:*

I would like to read all response headers using ajax, any idea how to do that?

8
  • Is it because the ajax call is erroring? Commented Feb 27, 2014 at 11:45
  • It doesn't matter, I do not get all headers with 200 code response either jsfiddle.net/tomasr/7jWSv/5 Commented Feb 27, 2014 at 11:49
  • It's probably because you are doing a cross domain access which jquery does not use XHR. Commented Feb 27, 2014 at 11:53
  • @Pilot Please explain more detailed. I thought that CORS is introduced to allow cross domain request and Access-Control-Allow-Origin is used for it. Commented Feb 27, 2014 at 11:56
  • try this type: "GET", async: false, complete: function (XMLHttpRequest, textStatus) { var headers = XMLHttpRequest.getAllResponseHeaders(); } Commented Feb 27, 2014 at 11:59

1 Answer 1

1

The problem solved by adding

Access-Control-Expose-Headers

to the server response and listing all headers like this

Access-Control-Expose-Headers: x-header-1, x-header-2, x-header-3

The response header above let Ajax request to read cross domain headers.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.