Javascript访问其他网页 [英] Javascript access another webpage
问题描述
我对JavaScript的了解非常少,但是我对编写一个需要来自另一个网页的信息的脚本感兴趣.是否有类似urllib2之类的javascript代码?它不需要非常强大,只需足以处理一个简单的GET请求,而无需存储cookie或任何东西并存储结果.
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.
推荐答案
有 XMLHttpRequest ,但由于相同的来源政策相同的来源政策.
There is the XMLHttpRequest, but that would be limited to the same domain of your web site, because of the Same Origin Policy.
但是,您可能有兴趣查看以下Stack Overflow帖子,以获取有关Same Origin策略的一些解决方案:
However, you may be interested in checking out the following Stack Overflow post for a few solutions around the Same Origin Policy:
更新:
这是一个非常基本的(非跨浏览器)示例:
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);
如果您在 Firebug 中运行以上命令,并且打开了堆栈溢出功能,则将获得该问题的HTML在您的JavaScript控制台中打印:
If you run the above in Firebug, with Stack Overflow open, you'd get the HTML of this question printed in your JavaScript console:
JavaScript访问其他网页http://img217.imageshack.us/img217/5545 /fbugxml.png
这篇关于Javascript访问其他网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!