6

I'm trying to read documentation and I must confess it is not an easy reading. I have no problem (after adding Access-Control-Allow-Origin header) to read responseText, but fail to get response header anywhere except Firefox.

So, my question is what is the right way to get response header, using cross-domain ajax?

I've tried to use (Access-Control-Expose-Headers), but, again, failed to read header.

2 Answers 2

16

So the way it should work is that you specify the headers you want the client to have access to in the Access-Control-Expose-Headers header. For example, if your server sets a Foo response header, and you want the client to be able to read it, your server should also send the following header:

Access-Control-Expose-Headers: Foo 

On the client side, you can read all the response headers by calling xhr.getAllResponseHeaders(). This returns the response headers as a string, which you can then parse into an object using the following code: https://gist.github.com/706839

That is an explanation of how things should work. However, note that there is a bug in older browsers where the response headers can't be read on the client. See here for more details: CORS xmlhttprequest HEAD method

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

3 Comments

Hi, I have added the following to server-side API. header(Access-Control-Allow-Headers: temp'); header('Access-Control-Expose-Headers: temp); header('temp: '. 12345); When I am making a cURL request to the API, I get the headers along with the response-data. But when I am making an AJAX call to the same API, I only get the data, without the header.
Anish, your comment sounds more appropriate as its own Stack Overflow question.
Is this question / answer relevant even today.
1

I had same problem, and found answer on Chromium mailing list that this is fixed in webkit, and it will be implemented in crhomium ~19.

I will try to find topic and update my answer.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.