开发者

read wikipedia url's content using jquery, cross domain network call

开发者 https://www.devze.com 2023-01-05 06:44 出处:网络
jQuery.ajax( { url:\'http://en.wikipedia.org/wiki/Football\', type:\'get\', dataType:\'jsonp\', success:function(data){alert(data);},
   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 ho开发者_JAVA百科w 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.


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);});


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.


You can use YQL for page fetching and get the JSONP response.

http://developer.yahoo.com/yql/console/#h=select%20*%20from%20html%20where%20url%3D%22http%3A//en.wikipedia.org/wiki/Football%22%0A

0

精彩评论

暂无评论...
验证码 换一张
取 消