Tomorrow I have an exam on Web Design and I can't seem to understand how to read JSONP responses with AJAX. I can do fine with JSON, as well as with JSONP as long as there is no callback function, but when the callback function has a name nothing seems to work.
I will give two examples of this:
FIRST EXAMPLE
Reading data from this link: https://openlibrary.org/api/books?bibkeys=ISBN%3A0451526538&callback=mycallback
My function:
$.ajax({
url: "https://openlibrary.org/api/books?bibkeys=ISBN:0451526538&callback=mycallback",
dataType: "jsonp",
success: function(data){
var isbn = data["ISBN:0451526538"].bib_key;
alert(isbn);
}
});
SECOND EXAMPLE
And this one I can't seem to understand WHY it does/doesn't work.
My way (which doesn't work):
$.ajax({
url: "http://www.flickr.com/services/feeds/photos_public.gne?tags=flowers&format=json#",
dataType: "jsonp",
success: function(data){
alert(data.title);
}
});
Correct code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
$.ajax({
url: "http://www.flickr.com/services/feeds/photos_public.gne?tags=flowers&format=json#",
dataType: "jsonp",
jsonp: "jsoncallback",
success: function(data){
alert(data.title);
}
});
Can anyone PLEASE take the time to explain how I can get past the wrapper (padding) that is the callback function to access the data. Please no $.getJSONP, this is the only way I have to get this to work that will be accepted in my exam.
</div>