4

I'm just wondering if there's a point to having a callback function on success in jQuery's getJSON function if I'm using JSONP.

From the documentation:

http://api.jquery.com/jQuery.getJSON/

jQuery.getJSON( url [, data] [, success(data, textStatus, jqXHR)] )

The "success" function is triggered after a successful JSON response.

However, if we use JSONP where the function callback is in the JSONP response, is there a need for the "success" function?

I'm guessing no, but I can't find any information on the page to confirm it. I just don't want to overlook some sort of security issue if there is a reason to use the "success" function.

Thanks!

2 Answers 2

2

jQuery automatically sets up its own callback function (with an autogenerated name) on the global window object and replaces the "?" in "callback=?" with the function's name. That callback function calls yours.

This means you can just pass an anonymous function to jQuery instead of having to make sure you don't use the same function name twice, just as with normal AJAX requests using jQuery.

Relevant quote from jQuery.ajax() documentation:

It is preferable to let jQuery generate a unique name as it'll make it easier to manage the requests and provide callbacks and error handling. You may want to specify the callback when you want to enable better browser caching of GET requests.

3
  • I read that but didn't fully understand what it was trying to say. And I wasn't sure if this note applied to getJSON for JSONP. I'll try and reread it with your added information and see if something clicks or not.
    – Zhao Li
    Commented Dec 29, 2012 at 21:54
  • I guess I still don't understand. getJSON is equivalent to $.ajax({ url: url, dataType: 'json', data: data, success: callback }); But the JSONP response will automatically call a function specified in the response. So I still don't see the use of the "success: callback" function. But after testing it out some, it might just be ignored anyways.
    – Zhao Li
    Commented Dec 31, 2012 at 7:44
  • I finally understand what you and the documentation was getting at. jQuery replaces "callback=?" with something like "callback=blah" so you can use anonymous functions. So at the end there is only just one callback/success function. Thanks!
    – Zhao Li
    Commented Jan 4, 2013 at 9:27
0

Of course you don't have to use the success callback but I can think of a good use of it - single responsibility.

The callback function passed via the JSONP url should only deal with parsing the JSON and altering the page dynamically while the success callback of jQuery can be used to perform other tasks related to the request itself rather than to the data received.

2
  • I really like the idea of separating responsibilities between the 2 callback functions, but I wasn't able to get it to work. Maybe jQuery just ignores the success callback?
    – Zhao Li
    Commented Dec 31, 2012 at 7:38
  • I finally understand what's happening. I commented on PleaseStand's answer. I hope I'm not further mistaken on some other point. Thanks!
    – Zhao Li
    Commented Jan 4, 2013 at 9:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.