antonpug antonpug - 7 months ago 53
HTTP Question

How can I get HTTP response header using JS?

I tried so many tutorials online but everything I try is really old and fails - I can't even create an XMLHTTPRequest object!

I just want to get the header from - how can I do that?


You would use xhr.getResponseHeader() to get a single header, or xhr.getAllResponseHeaders() to read all of the headers from an XMLHttpRequest response.

The reasons this won't work for you:

  • XMLHttpRequest is case sensitive. If you are using HTTP in all caps, it will fail.
  • Unless you are a google employee adding code to, your request falls victim to the same origin policy. You'll have to use your server as a proxy to get headers from a google request.