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?

2
  • 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.) Commented Mar 6, 2012 at 1:38
  • "Really old"? What have you tried? What "tutorials"? Which actual, proper, peer-reviewed books are you using? Commented Mar 6, 2012 at 1:45

2 Answers 2

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.
Sign up to request clarification or add additional context in comments.

Comments

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.

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.