Searching Wikipedia using API

后端 未结 3 1815
失恋的感觉
失恋的感觉 2020-12-23 12:15

I want to search Wikipedia using the query action. I am using this url:

http://en.wikipedia.org/w/api.php?action=query&format=json&list=search&a

3条回答
  •  生来不讨喜
    2020-12-23 12:37

    Here is the my solution:

    // Created By Pawan Mall | www.pawanmall.net
    $(document).ready(function() {
      $('#sTerm').focus();
      $('#resultArea').hide();
      $('#searchArticle').on('click', function() {
        $('#resultArea').show();
        searchTerm = $('#sTerm').val();
        let surl = 'https://en.wikipedia.org/w/api.php?action=query&prop=extracts&origin=*&format=json&generator=search&gsrnamespace=0&gsrlimit=1&gsrsearch=' + searchTerm;
        $.ajax({
          url: surl,
          header: {
            'Access-Control-Allow-Origin' : '*',
            'Content-Type': 'application/json'
          },
          method: 'GET',
          dataType: 'jsonp',
          data: '',
          beforeSend: function(){
            // $("#loader").show();
            $('#resultArea').html('
    ') }, success: function(data){ // console.log(data.query.pages); dataNum = Object.keys(data.query.pages)[0]; $('#resultArea').empty(); let newTitle = '

    '+data.query.pages[dataNum].title+'

    '; $('#resultArea').html(`${newTitle}
    ${data.query.pages[dataNum].extract}
    `); console.log(data); }, complete: function(){ $('#sTerm').val(''); $('#sTerm').focus(); } }); }); });
    
    
    

    Search Article on Wikipedia via Wikipedia Search API

提交回复
热议问题