使用jquery读取wikipedia url的内容,跨域网络调用

发布于 2024-09-07 02:40:58 字数 416 浏览 1 评论 0原文

   jQuery.ajax(
    {
      url:'http://en.wikipedia.org/wiki/Football',
      type:'get',
      dataType:'jsonp',
      success:function(data){alert(data);},
    }

我想使用 jQuery 从我的域中阅读维基百科页面,我正在按照上面的方式进行操作。 正如预期的那样,维基百科以纯 html 形式发送数据,但是当我们使用 $.ajax 获取跨域数据时,它期望收到的数据为 json 格式,因此我收到错误并且无法读取维基百科响应。

请建议我如何使用 jquery/javascript 读取维基百科网址(不涉及任何服务器端技术)还有任何可用的 api 可以通过它我从维基百科获取 json。

   jQuery.ajax(
    {
      url:'http://en.wikipedia.org/wiki/Football',
      type:'get',
      dataType:'jsonp',
      success:function(data){alert(data);},
    }

i want to read wikipedia page from my domain using jQuery, iam doing as above.
as expected wikipedia is sending data as pure html, but when we use $.ajax to get cross domain data it expects data received to be in json format so iam getting error and unable to read the wikiepedia response.

please suggest me how can i read wikipedia url using jquery/javascript (without involving any server side tech) also is there any api available through which i get json from wikipedia.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浮华 2024-09-14 02:40:58

有一个维基百科 API(更准确地说,维基百科的引擎 MediaWiki 有一个 API)。您可以在这里阅读更多相关信息:http://www.mediawiki.org/wiki/API

下面是一个关于如何获取“Football”页面的格式化内容的 jQuery 示例:

$.getJSON("http://en.wikipedia.org/w/api.php?action=parse&format=json&callback=?", {page:"Football", prop:"text"}, function(data) {console.log(data);});

There is a Wikipedia API (more precisely, MediaWiki, the engine of Wikipedia, has an API). You can read more about it here: http://www.mediawiki.org/wiki/API

Here is a jQuery example on how to fetch the formatted content of the "Football" page:

$.getJSON("http://en.wikipedia.org/w/api.php?action=parse&format=json&callback=?", {page:"Football", prop:"text"}, function(data) {console.log(data);});
残龙傲雪 2024-09-14 02:40:58

端点必须配置为服务 jsonp,但在本例中并非如此。它不会神奇地将普通的 html 响应类型转换为 jsonp 。您需要在服务器上创建一个代理,该代理将为您提供远程内容,例如,如果您使用 php,则查看此 链接

The endpoint has to be configured to serve jsonp which in this case it is not. It will not magically transform the normal html response type into jsonp for you. You will need to create a proxy on your server which will serve you the remote content for example if you are using php then check out this link.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文