JSONP and randomly generated callback function
Hello, I've several doubt about the
JSONP usage. I'm using JQuery to make JSONP requests and the documentation is quite confusing to me. I hope my question is not too long.
- In the specs it's written a JSONP call is always async. Is it a jQuery choice or the nature of JSONP?
- async:false would be simply ignored?
- If the jsonpCallback parameter is specified, this function will be executed when the data are retrieved. But right after, also the success callback will be executed. Can two callbacks co-exists? Is it the best usage pattern or should I just remove the success in this case?
- If the jsonpCallback is not specified a random callback function will be created and attached to the window object. Something like jQuery1360574548776335413_1776656584447. How does it work? Does it call the success callback passing to it its return data?
- Is the success the only available helper? The error and complete aren't available?
Here's my code:
- (function($) {
- var url = "https://www.googleapis.com/books/v1/volumes/zyTCAlFPjgYC";
- $.ajax({
- type: 'GET',
- url: url,
- // JSONP always async?
- async: false,
- jsonp: "callback",
- jsonpCallback: 'jsonCallback',
- contentType: "application/json",
- dataType: 'jsonp',
- //success always called even when jsonpCallback is specified?
- success: function(json) {
- console.dir(json);
- },
- // Error never called?
- error: function(e) {
- console.log(e.message);
- }
- });
- })(jQuery);
- function jsonCallback(json) {
- $(".test").html(json.volumeInfo.title);
- }
Thanks