I am trying to get the text from the page https://domains.google.com/checkip into a variable using only javascript. Is this possible?
Asked
Active
Viewed 71 times
0
-
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
-
1In 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 Answers
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
- 60,010
- 15
- 145
- 220

Patrick Roberts
- 49,224
- 10
- 102
- 153