javascript:如何获取网页的内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5299646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript: how to fetch the content of a web page
提问by tic
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?
在 JS 中是否可以获取网页的内容并将其分配给变量?例如,为什么下面的玩具代码不起作用?
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?
有没有更好的方法/代码?
回答by gion_13
use a server-side proxy like a php-page that reads the desired page and then make ajax calls to that proxy through javascript :
使用服务器端代理,如读取所需页面的 php 页面,然后通过 javascript 对该代理进行 ajax 调用:
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);
}
回答by Hux
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.
以上是行不通的,因为出于安全考虑,Ajax 请求无法访问其他域上的文件/页面。通常,您可以使用 [在此处插入服务器端语言] 制作脚本来下载请求的页面。然后您的 javascript 可以向此页面发出请求。
There is also 'JSONP', but this is typically used on sites that provide specific JSONP access, which most random URL's do not.
还有“JSONP”,但这通常用于提供特定 JSONP 访问的站点,而大多数随机 URL 不提供。
回答by SLaks
For security reasons, you cannot use AJAX to send a request to a different domain.
出于安全原因,您不能使用 AJAX 将请求发送到不同的域。
回答by Zlatko
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).
如果你真的需要这样做,你可以尝试使用 jQuery 和 iFrames(阅读更多内容(阅读更多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.
此外,您可以尝试使用 Access-Control-Allow-Origin: http://yourdomain:1234/在标题中,谷歌跨源资源共享。虽然它相对较新,但并非所有浏览器都知道这一点。这也取决于您是否可以控制其他服务器标头生成和其他一些事情。