3 var jsc = jQuery.now(),
4 jsre = /(\=)\?(&|$)|\?\?/i;
6 // Default jsonp settings
9 jsonpCallback: function() {
10 return jQuery.expando + "_" + ( jsc++ );
14 // Detect, normalize options and install callbacks for jsonp requests
15 jQuery.ajaxPrefilter( "json jsonp", function( s, originalSettings, jqXHR ) {
17 var dataIsString = ( typeof s.data === "string" );
19 if ( s.dataTypes[ 0 ] === "jsonp" ||
20 originalSettings.jsonpCallback ||
21 originalSettings.jsonp != null ||
22 s.jsonp !== false && ( jsre.test( s.url ) ||
23 dataIsString && jsre.test( s.data ) ) ) {
25 var responseContainer,
26 jsonpCallback = s.jsonpCallback =
27 jQuery.isFunction( s.jsonpCallback ) ? s.jsonpCallback() : s.jsonpCallback,
28 previous = window[ jsonpCallback ],
31 replace = "$1" + jsonpCallback + "$2",
32 cleanUp = function() {
33 // Set callback back to previous value
34 window[ jsonpCallback ] = previous;
35 // Call if it was a function and we have a response
36 if ( responseContainer && jQuery.isFunction( previous ) ) {
37 window[ jsonpCallback ]( responseContainer[ 0 ] );
41 if ( s.jsonp !== false ) {
42 url = url.replace( jsre, replace );
43 if ( s.url === url ) {
45 data = data.replace( jsre, replace );
47 if ( s.data === data ) {
48 // Add callback manually
49 url += (/\?/.test( url ) ? "&" : "?") + s.jsonp + "=" + jsonpCallback;
58 window[ jsonpCallback ] = function( response ) {
59 responseContainer = [ response ];
62 // Install cleanUp function
63 jqXHR.then( cleanUp, cleanUp );
65 // Use data converter to retrieve json after script execution
66 s.converters["script json"] = function() {
67 if ( !responseContainer ) {
68 jQuery.error( jsonpCallback + " was not called" );
70 return responseContainer[ 0 ];
73 // force json dataType
74 s.dataTypes[ 0 ] = "json";