3 var jsc = jQuery.now(),
4 jsre = /(\=)(?:\?|%3F)(&|$)|()(?:\?\?|%3F%3F)()/i;
6 // Default jsonp settings
9 jsonpCallback: function() {
10 return "jsonp" + jsc++;
14 // Detect, normalize options and install callbacks for jsonp requests
15 // (dataIsString is used internally)
16 jQuery.ajaxPrefilter("json jsonp", function(s, originalSettings, dataIsString) {
18 dataIsString = ( typeof(s.data) === "string" );
20 if ( s.dataTypes[ 0 ] === "jsonp" ||
21 originalSettings.jsonpCallback ||
22 originalSettings.jsonp != null ||
23 s.jsonp !== false && ( jsre.test( s.url ) ||
24 dataIsString && jsre.test( s.data ) ) ) {
26 var responseContainer,
27 jsonpCallback = s.jsonpCallback =
28 jQuery.isFunction( s.jsonpCallback ) ? s.jsonpCallback() : s.jsonpCallback,
29 previous = window[ jsonpCallback ],
32 replace = "$1" + jsonpCallback + "$2";
34 if ( s.jsonp !== false ) {
35 url = url.replace( jsre, replace );
36 if ( s.url === url ) {
38 data = data.replace( jsre, replace );
40 if ( s.data === data ) {
41 // Add callback manually
42 url += (/\?/.test( url ) ? "&" : "?") + s.jsonp + "=" + jsonpCallback;
50 window [ jsonpCallback ] = function( response ) {
51 responseContainer = [response];
54 s.complete = [function() {
56 // Set callback back to previous value
57 window[ jsonpCallback ] = previous;
59 // Call if it was a function and we have a response
61 if ( responseContainer && jQuery.isFunction ( previous ) ) {
62 window[ jsonpCallback ] ( responseContainer[0] );
65 // else, more memory leak avoidance
66 try{ delete window[ jsonpCallback ]; } catch(e){}
71 // Use data converter to retrieve json after script execution
72 s.converters["script json"] = function() {
73 if ( ! responseContainer ) {
74 jQuery.error( jsonpCallback + " was not called" );
76 return responseContainer[ 0 ];
79 // force json dataType
80 s.dataTypes[ 0 ] = "json";