JavaScript从Wikipeida API中提取某些数据

时间:2015-05-14 16:47:48

标签: javascript json api jsonp

如何从维基百科API中提取突出显示的(绿色)数据并显示在前端。我想将这些值分开并在前端单独显示。

enter image description here

function getJSONP(url, success) {
    var ud = '_' + +new Date,
        script = document.createElement('script'),
        head = document.getElementsByTagName('head')[0] || document.documentElement;

    window[ud] = function(data) {
        head.removeChild(script);
        success && success(data);
    };

    script.src = url.replace('callback=?', 'callback=' + ud);
    head.appendChild(script);

}
getJSONP('http://en.wikipedia.org/w/api.php?format=json&action=query&titles=List_of_metropolitan_areas_by_population&prop=revisions&rvprop=content&callback=?', function(data){
    console.log(data);
    document.getElementById("title").innerHTML = data.query.pages[125279].title;
    
    var inContent = data.query.pages[125279].revisions[0];
    document.getElementById("con").innerHTML = inContent;
});  
<div id="title"></div>
<div id="con"></div>

0 个答案:

没有答案