Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
132
rated 0 times [  134] [ 2]  / answers: 1 / hits: 15806  / 8 Years ago, mon, may 2, 2016, 12:00:00

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.


More From » jquery

 Answers
2

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

[#62334] Thursday, April 28, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
hector

Total Points: 726
Total Questions: 103
Total Answers: 100

Location: Seychelles
Member since Mon, Jun 28, 2021
3 Years ago
;