antonpug antonpug - 1 year ago 248
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?

Answer Source

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.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download