2010-12-09 19:34:28 +01:00
|
|
|
(function( jQuery ) {
|
|
|
|
|
|
|
|
var jsc = jQuery.now(),
|
2011-02-24 06:37:37 +01:00
|
|
|
jsre = /(\=)\?(&|$)|\?\?/i;
|
2010-12-09 19:34:28 +01:00
|
|
|
|
2011-01-09 21:19:27 +01:00
|
|
|
// Default jsonp settings
|
|
|
|
jQuery.ajaxSetup({
|
|
|
|
jsonp: "callback",
|
|
|
|
jsonpCallback: function() {
|
2011-01-29 01:25:27 +01:00
|
|
|
return jQuery.expando + "_" + ( jsc++ );
|
2011-01-09 21:19:27 +01:00
|
|
|
}
|
2011-01-20 04:12:15 +01:00
|
|
|
});
|
2010-12-09 19:34:28 +01:00
|
|
|
|
2011-01-16 02:57:39 +01:00
|
|
|
// Detect, normalize options and install callbacks for jsonp requests
|
2011-02-01 16:00:30 +01:00
|
|
|
jQuery.ajaxPrefilter( "json jsonp", function( s, originalSettings, jqXHR ) {
|
2011-01-16 05:26:46 +01:00
|
|
|
|
2011-01-31 18:53:44 +01:00
|
|
|
var dataIsString = ( typeof s.data === "string" );
|
2011-01-05 22:41:23 +01:00
|
|
|
|
2011-01-09 21:19:27 +01:00
|
|
|
if ( s.dataTypes[ 0 ] === "jsonp" ||
|
|
|
|
originalSettings.jsonpCallback ||
|
2011-01-16 05:26:46 +01:00
|
|
|
originalSettings.jsonp != null ||
|
|
|
|
s.jsonp !== false && ( jsre.test( s.url ) ||
|
|
|
|
dataIsString && jsre.test( s.data ) ) ) {
|
2010-12-09 19:34:28 +01:00
|
|
|
|
2011-01-16 02:57:39 +01:00
|
|
|
var responseContainer,
|
|
|
|
jsonpCallback = s.jsonpCallback =
|
2010-12-09 19:34:28 +01:00
|
|
|
jQuery.isFunction( s.jsonpCallback ) ? s.jsonpCallback() : s.jsonpCallback,
|
2011-01-16 02:57:39 +01:00
|
|
|
previous = window[ jsonpCallback ],
|
2011-01-16 05:26:46 +01:00
|
|
|
url = s.url,
|
|
|
|
data = s.data,
|
2011-01-31 18:53:44 +01:00
|
|
|
replace = "$1" + jsonpCallback + "$2",
|
|
|
|
cleanUp = function() {
|
|
|
|
// Set callback back to previous value
|
|
|
|
window[ jsonpCallback ] = previous;
|
|
|
|
// Call if it was a function and we have a response
|
|
|
|
if ( responseContainer && jQuery.isFunction( previous ) ) {
|
|
|
|
window[ jsonpCallback ]( responseContainer[ 0 ] );
|
|
|
|
}
|
|
|
|
};
|
2011-01-16 05:26:46 +01:00
|
|
|
|
|
|
|
if ( s.jsonp !== false ) {
|
|
|
|
url = url.replace( jsre, replace );
|
|
|
|
if ( s.url === url ) {
|
|
|
|
if ( dataIsString ) {
|
|
|
|
data = data.replace( jsre, replace );
|
|
|
|
}
|
|
|
|
if ( s.data === data ) {
|
|
|
|
// Add callback manually
|
|
|
|
url += (/\?/.test( url ) ? "&" : "?") + s.jsonp + "=" + jsonpCallback;
|
|
|
|
}
|
|
|
|
}
|
2010-12-09 19:34:28 +01:00
|
|
|
}
|
2011-01-05 22:41:23 +01:00
|
|
|
|
2010-12-09 19:34:28 +01:00
|
|
|
s.url = url;
|
|
|
|
s.data = data;
|
2011-01-05 22:41:23 +01:00
|
|
|
|
2011-01-31 18:53:44 +01:00
|
|
|
// Install callback
|
2011-01-23 05:51:41 +01:00
|
|
|
window[ jsonpCallback ] = function( response ) {
|
|
|
|
responseContainer = [ response ];
|
2011-01-16 02:57:39 +01:00
|
|
|
};
|
2011-01-05 22:41:23 +01:00
|
|
|
|
2011-01-31 18:53:44 +01:00
|
|
|
// Install cleanUp function
|
2011-02-01 16:00:30 +01:00
|
|
|
jqXHR.then( cleanUp, cleanUp );
|
2011-01-05 22:41:23 +01:00
|
|
|
|
2011-01-16 02:57:39 +01:00
|
|
|
// Use data converter to retrieve json after script execution
|
|
|
|
s.converters["script json"] = function() {
|
2011-01-31 18:53:44 +01:00
|
|
|
if ( !responseContainer ) {
|
2011-01-16 02:57:39 +01:00
|
|
|
jQuery.error( jsonpCallback + " was not called" );
|
|
|
|
}
|
|
|
|
return responseContainer[ 0 ];
|
|
|
|
};
|
2011-01-05 22:41:23 +01:00
|
|
|
|
2011-01-16 02:57:39 +01:00
|
|
|
// force json dataType
|
|
|
|
s.dataTypes[ 0 ] = "json";
|
2011-01-09 21:19:27 +01:00
|
|
|
|
2011-01-16 02:57:39 +01:00
|
|
|
// Delegate to script
|
|
|
|
return "script";
|
|
|
|
}
|
2011-01-23 05:51:41 +01:00
|
|
|
} );
|
2010-12-09 19:34:28 +01:00
|
|
|
|
2010-12-10 03:14:03 +01:00
|
|
|
})( jQuery );
|