0

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 google.com - how can I do that?

antonpug
  • 11,964
  • 27
  • 76
  • 120
  • 1
    Making a request to google is going to violate the cross host restriction. You cannot request a page on a hostname other than your site's. (Though you can do so in round about ways -- just not with a XMLHTTPRequestObject.) – Corbin Mar 06 '12 at 01:38
  • "Really old"? What have you tried? What "tutorials"? Which actual, proper, peer-reviewed _books_ are you using? – Lightness Races in Orbit Mar 06 '12 at 01:45

2 Answers2

3

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 google.com, 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.
Luke
  • 5,047
  • 4
  • 31
  • 58
gilly3
  • 78,870
  • 23
  • 132
  • 169
0

You cant simply do this by JS. You'll have to use AJAX and do a server request to PHP,ASP, Java or whatever. The XMLHTTPRequest should do it - if you really want to do it manually. But it will really not work with foreign domains, so you are forced to do the XMLHTTTPRequest to a page on your server which will deliver the header.

androidavid
  • 1,088
  • 1
  • 7
  • 17