Is there any way to specify the value of the callback parameter when using JQuery's getJSON method?
For example:
$.getJSON('/content?callback=?', function(data) {});
Generates the following URL:
content?callback=jQuery15108431726952168015_1299633045933&_=1299633046552
The parameter here is generated randomly by the library.
I would like to specify my own callback parameter.
I would like to use the same callback parameter for every request so I can aggresively cache the response.
You need to be at least on jQuery 1.5 for this to work.
// The URL generated is "/content?callback=myCallback"
$.ajax({
url: '/content?callback=?',
dataType: 'jsonp',
jsonpCallback: 'myCallback',
cache: true,
success: function(data) {}
});
All of jQuery's ajax convenience functions are just wrappers for $.ajax.
$.ajax({
url: url,
dataType: 'json',
data: data,
success: callback
});
Why not create your own convenience wrapper?
function fetchJSON(url, data, callback) {
return jQuery.get(url, data, callback, "json");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With