0

I am trying to get the text from the page https://domains.google.com/checkip into a variable using only javascript. Is this possible?

zigzag32
  • 9
  • 1
  • here is a related question that should do what you need. – Barett Jun 22 '15 at 22:55
  • possible duplicate of [Open webpage and parse it using JavaScript](http://stackoverflow.com/questions/597907/open-webpage-and-parse-it-using-javascript) – Barett Jun 22 '15 at 22:55
  • 1
    In general this is not possible because of [Same-Origin Policy](https://en.wikipedia.org/wiki/Same-origin_policy). However, *if and only if* the foreign site participates in [CORS](https://en.wikipedia.org/wiki/Cross-origin_resource_sharing) (for the requested resource) then it's possible to use a cross-domain AJAX request. – user2864740 Jun 22 '15 at 22:55
  • @user2864740 it does not participate in CORS, by design, as stated in my answer. – Patrick Roberts Jun 22 '15 at 22:57
  • Why make things more complicated? This is a very general task in web-development and usually gets done via AJAX request to some internal server-side script that grabs the data from remote hosts. – Yang Jun 22 '15 at 23:03

1 Answers1

4

No it's not possible because that page (https://domains.google.com/checkip) prevents cross-domain requests by not placing an Access-Control-Allow-Origin header in the HTTP response.

You can't even put the page in an iframe because the header x-frame-options: SAMEORIGIN prevents it from being embedded by compliant browsers as well.

user2864740
  • 54,112
  • 10
  • 112
  • 187
Patrick Roberts
  • 40,065
  • 5
  • 74
  • 116