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
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