使用Wikipedia的API从搜索查询中获取结果

时间:2016-05-02 14:38:17

标签: javascript jquery json wikipedia-api

我正在尝试使用维基百科的API进行搜索查询,然后将这些结果附加到我的页面。这就是我到目前为止所做的:

 "use strict";
$(document).ready(function(){

function searchWikipedia(searchCriteria){
    $.getJSON('https://en.wikipedia.org/w/api.php?action=query&format=json&limit=15&callback=?&titles=' + searchCriteria, processResult);

}


$('#btn').click(function searchCriteria() {
    var searchCriteria = $("input[name=Wikipedia]").val();
    searchWikipedia(searchCriteria);

})

function processResult(apiResult){

  if (apiResult.query.pages[-1]){
       console.log("No results");
    } else {
       for (var i = 0; i < apiResult.length; i++){
             $('#display-result').append('<p>'+apiResult+'</p>');
       }
   }

}
}); 

到目前为止,没有任何内容附加到我的HTML中,并且我的控制台中没有错误。

5 个答案:

答案 0 :(得分:6)

答案 1 :(得分:2)

答案 2 :(得分:1)

试试这个样本:

$(document).ready(function(){

    $('#btn').click(function() {

        $.ajax({
            url: '//en.wikipedia.org/w/api.php',
            data: { action: 'query', list: 'search', srsearch: $("input[name=Wikipedia]").val(), format: 'json' },
            dataType: 'jsonp',
            success: function (x) {
                console.log('title', x.query.search[0].title);
            }
        });
    });
}); 

Source

答案 3 :(得分:1)

这将显示图片

的查询结果
$(document).ready(function(){
  $('#btn').click(function() {
     var search_text = $("input[name=Wikipedia]").val();
     var url = 'https://en.wikipedia.org/w/api.php?format=json&action=query&generator=search&gsrnamespace=0&gsrlimit=10&prop=pageimages|extracts&pilimit=max&exintro&explaintext&exsentences=1&exlimit=max&gsrsearch=';
     $.getJSON( url + search_text +'&callback=?',function(data){        
      for(var key in data.query.pages){
        var titleArt = data.query.pages[key].title;
        var extractArt = data.query.pages[key].extract;
        var linkArt = 'https://en.wikipedia.org/?curid=' + data.query.pages[key].pageid;
        var imgArt;

        if(data.query.pages[key].hasOwnProperty('thumbnail')){
          imgArt = data.query.pages[key].thumbnail.source;
        } else {
          imgArt = 'http://www.wallpaperup.com/uploads/wallpapers/2014/04/02/319530/big_thumb_e96d0c33f97706bc093572bc613cb23d.jpg';
        }

        var contentHTML = '<div class="col-md-4"><div class="box-result"><div class="bg-result"></div><a href="' +  linkArt + '" target="_blank"><div class="box-content center-block"><div class="article-thumbnail"><img src="' + imgArt + '" alt="" /></div><h1>'+ titleArt +'</h1><p>' + extractArt + '</p></div></a></div></div>';
        $('#display-result').append(contentHTML);
     }      
  });
 });
}); 

答案 4 :(得分:0)

function Wiki(lang) {
  this.lang = lang || "fr";
  this.inuse = false;
}

Wiki.prototype.research = function(s, callback) {
  if (this.inuse) {
    console.error("Wiki est déjà en cours d'utilisation !");
  } else {
    this.inuse = true;
    var r = new XMLHttpRequest();
    r.onload = function() {
      Wiki.prototype.inuse = false;
      var j = JSON.parse(r.responseText);
      callback(j);
    }
    r.open('GET', "https://" + this.lang + ".wikipedia.org/w/api.php?%20action=opensearch&format=json&origin=*&profile=normal&search=" + encodeURIComponent(s));
    r.send();
  }
}

var c = new Wiki();
c.research("Victor Hugo", function(result) {
  console.log(result);
});

//EXEMPLE
var c = new Wiki("en");
c.research("Victor Hugo", function(result) {
  console.log(result);
}