What is the getJSON callback for JSONP?

I'm just wondering if it makes sense to have a callback function when the jQuery getJSON function succeeds if I use JSONP.

From the documentation:

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

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

The success function is executed after a successful JSON response.

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

I suppose not, but I cannot find any information on the page to confirm it. I just don’t want to lose sight of any security problem if there is a reason to use the Success function.

Thanks!

+4
source share
2 answers

jQuery automatically installs its own callback function (with an auto-generated name) in the global window object and replaces "?" in "callback =?" with the name of the function. This callback function calls yours.

This means that you can simply pass an anonymous jQuery function instead of making sure you are not using the same function name twice as regular AJAX queries using jQuery.

Relevant quote from jQuery.ajax () Documentation :

It is preferable that jQuery generate a unique name, as it would simplify query management and provide callbacks and error handling. You can specify a callback if you want to enable better browser caching of GET requests.

+2
source

Of course, you do not need to use the success callback, but I can think of its good use - the only responsibility.

The callback function passed through the JSONP URL should only process JSON parsing and page changes dynamically, while the jQuery callback can be used to perform other tasks related to the request itself rather than the received data.

0
source

All Articles