Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Wikipedia's API to fetch results from search query

I am trying to use Wikipedia's API to make a search query, then append those results to my page. This is what I have so far :

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

}
}); 

So far nothing appends to my html and there's no errors in my console.

like image 505
victoria Avatar asked May 02 '16 14:05

victoria


People also ask

What can you do with Wikipedia API?

Introduction. The MediaWiki Action API is a web service that allows access to some wiki features like authentication, page operations, and search. It can provide meta information about the wiki and the logged-in user.

How do I access my API on Wikipedia?

For example, the English Wikipedia is at https://en.wikipedia.org/w/api.php . For testing new accounts or test edits to pages, use the test wiki endpoint https://test.wikipedia.org/w/api.php .

Does Wikipedia have APIS?

What is the Wikipedia API? The Wikipedia API (official documentation) is supported by the MediaWiki's API and provide access to Wikipedia and other MediaWiki data without interacting with the user interface.

How does Wikipedia get content?

Written collaboratively by largely anonymous volunteers, anyone with Internet access and in good standing can write and make changes to Wikipedia articles (except in limited cases where editing is restricted to prevent disruption or vandalism).


2 Answers

@Ali Mamedov's answer is the way to go (it's from Wikipedia) But the wikipedia link is missing the http:. Also, you can handle the response on your function:

   $(document).ready(function(){
        $('#btn').click(function() {
            $.ajax({
                url: 'http://en.wikipedia.org/w/api.php',
                data: { action: 'query', list: 'search', srsearch: $("input[name=Wikipedia]").val(), format: 'json' },
                dataType: 'jsonp',
                success: processResult
            });
        });
    }); 

  function processResult(apiResult){
     for (var i = 0; i < apiResult.query.search.length; i++){
          $('#display-result').append('<p>'+apiResult.query.search[i].title+'</p>');
     }
  }
like image 58
Bruno Garcia Avatar answered Oct 27 '22 00:10

Bruno Garcia


 <script type="text/javascript">
               

            $.ajax({
                type: "GET",
                url: "http://en.wikipedia.org/w/api.php?action=opensearch&search=" + txt + "&callback=?",
                contentType: "application/json; charset=utf-8",
                async: false,
                dataType: "json",
                success: function (data, textStatus, jqXHR) {
                    $.each(data, function (i, item) {
                        if (i == 1) {
                            var searchData = item[0];
                            WikipediaAPIGetContent(searchData);
                        }
                    });
                },
                error: function (errorMessage) {
                    alert(errorMessage);
                }
            });
        }

        function WikipediaAPIGetContent(search) {
            $.ajax({
                type: "GET",
                url: "http://en.wikipedia.org/w/api.php?action=parse&format=json&prop=text&section=0&page=" + search + "&callback=?",
                contentType: "application/json; charset=utf-8",
                async: false,
                dataType: "json",
                success: function (data, textStatus, jqXHR) {

                    var markup = data.parse.text["*"];
                    var blurb = $('<div></div>').html(markup);

                    // remove links as they will not work
                    blurb.find('a').each(function () { $(this).replaceWith($(this).html()); });

                    // remove any references
                    blurb.find('sup').remove();

                    // remove cite error
                    blurb.find('.mw-ext-cite-error').remove();
                    $('#results').html($(blurb).find('p'));
                    $('#results').html(blurb);

                },
                error: function (errorMessage) {
                    alert(errorMessage);
                }
            });
        }
    </script>
like image 28
J. Doe Avatar answered Oct 26 '22 22:10

J. Doe