hi, i’m in the challenge “Wikipedia viewer” but i have a problem with the API. actually i use this API " https://en.wikipedia.org/w/api.php?action=opensearch&search="+ term +"&format=json&callback=?" and not return nothing.
Can someone help me to observer mi code and tell me what is wrong?
$("#search").click(function(){
var term = $("#toSearch").val();
var url = "https://en.wikipedia.org/w/api.php?action=opensearch&search="+ term +"&format=json&callback=?";
$.ajax({
url: url,
type: "GET",
async: false,
dataType: "json",
success: function(data, status, jqXHR){
console.log(data);
},
error: function(request,error){
console.log (arguments);
alert ("error "+ error);
}
})
})
I’ve seen this error crop up a lot recently. I think it’s the way everyone is using .ajax. I find the quickest fix for this is to remove the callback from your url (which does work because I just copy/pasted it into a new browser tab and it returns data) and change dataType to “jsonp”.
Doing that will let jQuery do its thing. It will add the callback for you and remove it before shipping to the success method.
So, this:
$("#search").click(function(){
var term = $("#toSearch").val();
// remove callback from url
var url = "https://en.wikipedia.org/w/api.php?action=opensearch&search="+ term +"&format=json";
$.ajax({
url: url,
type: "GET",
async: false,
dataType: "jsonp", // change to `jsonp` here
success: function(data, status, jqXHR){
console.log(data);
},
error: function(request,error){
console.log (arguments);
alert ("error "+ error);
}
})
})