Javascript 访问另一个网页 [英] Javascript access another webpage

查看:59
本文介绍了Javascript 访问另一个网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 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,但由于同源政策.

但是,您可能有兴趣查看以下 Stack Overflow 帖子,了解有关同源策略的一些解决方案:

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 中运行上述内容,并打开 Stack Overflow,您将获得此问题的 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆