Take a look at this fiddle : http://jsfiddle.net/3JRU6/
$(document).ready(function(){
var url='http://api.worldbank.org/topic/4?per_page=10&format=json&callback=?';
var query;
$('button').click(function(){
$.getJSON(url,function(json){
$.each(json.results,function(i,data){
window.alert("found");
$("#results").append('<p>'+data.value+'</p>');
});
});
});
});
I want to connect to the worldbank’s opendata but when I hit the button, nothing happens. I’ve tried the same script with the twitter API and then it did work. The original link is without the &callback=? but I had to add it because I got an error.
Thanks in advance!
The
getJSONmethod will make a JSONP call if the URL contains a callback property.Ref: http://api.jquery.com/jQuery.getJSON/
The request works fine, and the data arrives to the browser, but as the response is JSON and not JSONP, the data is just discarded and the success callback method will not be called.
I tried to change
format=jsontoformat=jsonpin the URL, but the response is an error message:You have to check with your API provider on how to make a JSONP request instead of a JSON request.
Edit:
As Jimmy Oliger says, the API uses a
prefixproperty instead ofcallback. I tried this, and jQuery actually uses that property instead, and the success callback is called.The response is an array where the first item is paging information, and the second item is the array containing the data, so loop
json[1]to show the data:Demo: http://jsfiddle.net/Guffa/3JRU6/4/