Javascript 访问另一个网页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3315235/
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 access another webpage
提问by EricR
I know very, very little of javascript, but I'm interested in writing a script which needs information from another webpage. It there a javascript equivalent of something like urllib2? It doesn't need to be very robust, just enough to process a simple GET request, no need to store cookies or anything and store the results.
我对 javascript 知之甚少,但我对编写需要来自另一个网页的信息的脚本很感兴趣。它有一个相当于 urllib2 的 javascript 吗?它不需要非常健壮,足以处理一个简单的 GET 请求,不需要存储 cookie 或任何东西并存储结果。
采纳答案by Daniel Vassallo
There is the XMLHttpRequest, but that would be limited to the same domain of your web site, because of the Same Origin Policy.
有XMLHttpRequest,但由于Same Origin Policy,这将仅限于您网站的同一域。
However, you may be interested in checking out the following Stack Overflow post for a few solutions around the Same Origin Policy:
但是,您可能有兴趣查看以下 Stack Overflow 帖子,了解有关同源策略的一些解决方案:
UPDATE:
更新:
Here's a very basic (non cross-browser) example:
这是一个非常基本的(非跨浏览器)示例:
var xhr = new XMLHttpRequest();
xhr.open('GET', '/questions/3315235', true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
console.log(xhr.responseText);
}
};
xhr.send(null);
If you run the above in Firebug, with Stack Overflow open, you'd get the HTML of this question printed in your JavaScript console:
如果你在Firebug 中运行上面的代码,并打开 Stack Overflow,你会在 JavaScript 控制台中打印出这个问题的 HTML:
JavaScript access another webpage http://img217.imageshack.us/img217/5545/fbugxml.png
JavaScript 访问另一个网页 http://img217.imageshack.us/img217/5545/fbugxml.png
回答by Sojharo Mangi
Write your own server, which runs the script to load the data from websites. Then from your web page, ask your server to fetch the data from websites and send them back to you.
编写您自己的服务器,该服务器运行脚本以从网站加载数据。然后在您的网页上,要求您的服务器从网站获取数据并将其发送回给您。
回答by Josh K
You could issue an AJAX request and process it.
您可以发出 AJAX 请求并对其进行处理。

