ffffng/app/bower_components/jquery/src/ajax/jsonp.js

104 lines
2.7 KiB
JavaScript
Raw Normal View History

2017-05-13 13:25:33 +02:00
define( [
2014-05-12 20:08:19 +02:00
"../core",
2019-03-29 22:00:08 +01:00
"../var/isFunction",
2014-05-12 20:08:19 +02:00
"./var/nonce",
"./var/rquery",
"../ajax"
2019-03-29 22:00:08 +01:00
], function( jQuery, isFunction, nonce, rquery ) {
2014-05-12 20:08:19 +02:00
2017-05-13 13:25:33 +02:00
"use strict";
2014-05-12 20:08:19 +02:00
var oldCallbacks = [],
rjsonp = /(=)\?(?=&|$)|\?\?/;
// Default jsonp settings
2017-05-13 13:25:33 +02:00
jQuery.ajaxSetup( {
2014-05-12 20:08:19 +02:00
jsonp: "callback",
jsonpCallback: function() {
var callback = oldCallbacks.pop() || ( jQuery.expando + "_" + ( nonce++ ) );
this[ callback ] = true;
return callback;
}
2017-05-13 13:25:33 +02:00
} );
2014-05-12 20:08:19 +02:00
// Detect, normalize options and install callbacks for jsonp requests
jQuery.ajaxPrefilter( "json jsonp", function( s, originalSettings, jqXHR ) {
var callbackName, overwritten, responseContainer,
jsonProp = s.jsonp !== false && ( rjsonp.test( s.url ) ?
"url" :
2017-05-13 13:25:33 +02:00
typeof s.data === "string" &&
( s.contentType || "" )
.indexOf( "application/x-www-form-urlencoded" ) === 0 &&
rjsonp.test( s.data ) && "data"
2014-05-12 20:08:19 +02:00
);
// Handle iff the expected data type is "jsonp" or we have a parameter to set
if ( jsonProp || s.dataTypes[ 0 ] === "jsonp" ) {
// Get callback name, remembering preexisting value associated with it
2019-03-29 22:00:08 +01:00
callbackName = s.jsonpCallback = isFunction( s.jsonpCallback ) ?
2014-05-12 20:08:19 +02:00
s.jsonpCallback() :
s.jsonpCallback;
// Insert callback into url or form data
if ( jsonProp ) {
s[ jsonProp ] = s[ jsonProp ].replace( rjsonp, "$1" + callbackName );
} else if ( s.jsonp !== false ) {
s.url += ( rquery.test( s.url ) ? "&" : "?" ) + s.jsonp + "=" + callbackName;
}
// Use data converter to retrieve json after script execution
2017-05-13 13:25:33 +02:00
s.converters[ "script json" ] = function() {
2014-05-12 20:08:19 +02:00
if ( !responseContainer ) {
jQuery.error( callbackName + " was not called" );
}
return responseContainer[ 0 ];
};
2017-05-13 13:25:33 +02:00
// Force json dataType
2014-05-12 20:08:19 +02:00
s.dataTypes[ 0 ] = "json";
// Install callback
overwritten = window[ callbackName ];
window[ callbackName ] = function() {
responseContainer = arguments;
};
// Clean-up function (fires after converters)
2017-05-13 13:25:33 +02:00
jqXHR.always( function() {
// If previous value didn't exist - remove it
if ( overwritten === undefined ) {
jQuery( window ).removeProp( callbackName );
// Otherwise restore preexisting value
} else {
window[ callbackName ] = overwritten;
}
2014-05-12 20:08:19 +02:00
// Save back as free
if ( s[ callbackName ] ) {
2017-05-13 13:25:33 +02:00
// Make sure that re-using the options doesn't screw things around
2014-05-12 20:08:19 +02:00
s.jsonpCallback = originalSettings.jsonpCallback;
2017-05-13 13:25:33 +02:00
// Save the callback name for future use
2014-05-12 20:08:19 +02:00
oldCallbacks.push( callbackName );
}
// Call if it was a function and we have a response
2019-03-29 22:00:08 +01:00
if ( responseContainer && isFunction( overwritten ) ) {
2014-05-12 20:08:19 +02:00
overwritten( responseContainer[ 0 ] );
}
responseContainer = overwritten = undefined;
2017-05-13 13:25:33 +02:00
} );
2014-05-12 20:08:19 +02:00
// Delegate to script
return "script";
}
2017-05-13 13:25:33 +02:00
} );
2014-05-12 20:08:19 +02:00
2017-05-13 13:25:33 +02:00
} );