javascript: how to fetch the content of a web page
Asked Answered
S

4

11

In JS is it possible to fetch the content of a web page assigning it to a variable? For example, why the following toy code does not work?

var req = new XMLHttpRequest();
req.open('GET', 'http://www.google.com', false);
req.send(null);
if(req.status == 200)
  alert(req.responseText);

Is there a better method/code?

Severity answered 14/3, 2011 at 14:5 Comment(1)
en.wikipedia.org/wiki/Same_origin_policyDuodenal
R
12

use a server-side proxy like a php-page that reads the desired page and then make ajax calls to that proxy through javascript :

var req = new XMLHttpRequest();

req.open('GET', 'proxy.php?url=http://www.google.com', false);
req.send(null);

if(req.status == 200) {
   alert(req.responseText);
}
Rexanne answered 14/3, 2011 at 14:36 Comment(0)
L
8

The above does not work, because Ajax requests cannot access files/pages on other domains, due to security concerns. Typically, you can make a script using [Insert Server Side Language here] to download the requested page. Then your javascript can make a request to this page.

There is also 'JSONP', but this is typically used on sites that provide specific JSONP access, which most random URL's do not.

Lagunas answered 14/3, 2011 at 14:9 Comment(0)
G
6

For security reasons, you cannot use AJAX to send a request to a different domain.

Gewgaw answered 14/3, 2011 at 14:8 Comment(0)
L
2

If you really need to do this, you can try using jQuery and iFrames (read more at (read more http://softwareas.com/cross-domain-communication-with-iframes).

Also, you can try with Access-Control-Allow-Origin: http://yourdomain:1234/ in headers, google for Cross-Origin Resource Sharing. It's relativelly new though, not all browsers know about this. That also depends if you have the control of the other server headers generation and few other things.

Lukey answered 14/3, 2011 at 14:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.