wikipedia API JSON isn't recognized by $.getJSON() method

混江龙づ霸主 提交于 2019-12-11 01:18:59

问题


I know already that this method works for other JSON formatted data but not the wikipedia API JSON output as listed here. Any help would be great:

$.getJSON('https://en.wikipedia.org/w/api.php?format=json&action=query&generator=search&gsrsearch=starwars', function(data) {
    $("p").html(JSON.stringify(data));
});

回答1:


Add callback

$.getJSON('https://en.wikipedia.org/w/api.php?format=json&action=query&generator=search&gsrsearch=starwars&callback=?', function(data) {
    console.log(data);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>



回答2:


Another way is to $.ajax with a jsonp dataType:

jQuery(document).ready(function($) {
  $.ajax({
    url: "https://en.wikipedia.org/w/api.php?format=json&action=query&generator=search&gsrsearch=starwars",
    dataType: "jsonp",
    success: function(data) {
      $("pre").html(JSON.stringify(data, null, 3));
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre></pre>



回答3:


Jaromanda X is right, you'll need to use a callback function to sort this out. https://www.mediawiki.org/wiki/API:Cross-site_requests/en



来源:https://stackoverflow.com/questions/34574602/wikipedia-api-json-isnt-recognized-by-getjson-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!