| 1 |
(function( jQuery ) { |
| 2 |
|
| 3 |
var jsc = jQuery.now(), |
| 4 |
jsre = /(\=)\?(&|$)|\?\?/i; |
| 5 |
|
| 6 |
// Default jsonp settings |
| 7 |
jQuery.ajaxSetup({ |
| 8 |
jsonp: "callback", |
| 9 |
jsonpCallback: function() { |
| 10 |
return jQuery.expando + "_" + ( jsc++ ); |
| 11 |
} |
| 12 |
}); |
| 13 |
|
| 14 |
// Detect, normalize options and install callbacks for jsonp requests |
| 15 |
jQuery.ajaxPrefilter( "json jsonp", function( s, originalSettings, jqXHR ) { |
| 16 |
|
| 17 |
var inspectData = s.contentType === "application/x-www-form-urlencoded" && |
| 18 |
( typeof s.data === "string" ); |
| 19 |
|
| 20 |
if ( s.dataTypes[ 0 ] === "jsonp" || |
| 21 |
s.jsonp !== false && ( jsre.test( s.url ) || |
| 22 |
inspectData && jsre.test( s.data ) ) ) { |
| 23 |
|
| 24 |
var responseContainer, |
| 25 |
jsonpCallback = s.jsonpCallback = |
| 26 |
jQuery.isFunction( s.jsonpCallback ) ? s.jsonpCallback() : s.jsonpCallback, |
| 27 |
previous = window[ jsonpCallback ], |
| 28 |
url = s.url, |
| 29 |
data = s.data, |
| 30 |
replace = "$1" + jsonpCallback + "$2"; |
| 31 |
|
| 32 |
if ( s.jsonp !== false ) { |
| 33 |
url = url.replace( jsre, replace ); |
| 34 |
if ( s.url === url ) { |
| 35 |
if ( inspectData ) { |
| 36 |
data = data.replace( jsre, replace ); |
| 37 |
} |
| 38 |
if ( s.data === data ) { |
| 39 |
// Add callback manually |
| 40 |
url += (/\?/.test( url ) ? "&" : "?") + s.jsonp + "=" + jsonpCallback; |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
s.url = url; |
| 46 |
s.data = data; |
| 47 |
|
| 48 |
// Install callback |
| 49 |
window[ jsonpCallback ] = function( response ) { |
| 50 |
responseContainer = [ response ]; |
| 51 |
}; |
| 52 |
|
| 53 |
// Clean-up function |
| 54 |
jqXHR.always(function() { |
| 55 |
// Set callback back to previous value |
| 56 |
window[ jsonpCallback ] = previous; |
| 57 |
// Call if it was a function and we have a response |
| 58 |
if ( responseContainer && jQuery.isFunction( previous ) ) { |
| 59 |
window[ jsonpCallback ]( responseContainer[ 0 ] ); |
| 60 |
} |
| 61 |
}); |
| 62 |
|
| 63 |
// Use data converter to retrieve json after script execution |
| 64 |
s.converters["script json"] = function() { |
| 65 |
if ( !responseContainer ) { |
| 66 |
jQuery.error( jsonpCallback + " was not called" ); |
| 67 |
} |
| 68 |
return responseContainer[ 0 ]; |
| 69 |
}; |
| 70 |
|
| 71 |
// force json dataType |
| 72 |
s.dataTypes[ 0 ] = "json"; |
| 73 |
|
| 74 |
// Delegate to script |
| 75 |
return "script"; |
| 76 |
} |
| 77 |
}); |
| 78 |
|
| 79 |
})( jQuery ); |