1 import { jQuery } from "../core.js";
2 import { nonce } from "./var/nonce.js";
3 import { rquery } from "./var/rquery.js";
8 rjsonp = /(=)\?(?=&|$)|\?\?/;
10 // Default jsonp settings
13 jsonpCallback: function() {
14 var callback = oldCallbacks.pop() || ( jQuery.expando + "_" + ( nonce.guid++ ) );
15 this[ callback ] = true;
20 // Detect, normalize options and install callbacks for jsonp requests
21 jQuery.ajaxPrefilter( "jsonp", function( s, originalSettings, jqXHR ) {
23 var callbackName, overwritten, responseContainer,
24 jsonProp = s.jsonp !== false && ( rjsonp.test( s.url ) ?
26 typeof s.data === "string" &&
27 ( s.contentType || "" )
28 .indexOf( "application/x-www-form-urlencoded" ) === 0 &&
29 rjsonp.test( s.data ) && "data"
32 // Get callback name, remembering preexisting value associated with it
33 callbackName = s.jsonpCallback = typeof s.jsonpCallback === "function" ?
37 // Insert callback into url or form data
39 s[ jsonProp ] = s[ jsonProp ].replace( rjsonp, "$1" + callbackName );
40 } else if ( s.jsonp !== false ) {
41 s.url += ( rquery.test( s.url ) ? "&" : "?" ) + s.jsonp + "=" + callbackName;
44 // Use data converter to retrieve json after script execution
45 s.converters[ "script json" ] = function() {
46 if ( !responseContainer ) {
47 jQuery.error( callbackName + " was not called" );
49 return responseContainer[ 0 ];
52 // Force json dataType
53 s.dataTypes[ 0 ] = "json";
56 overwritten = window[ callbackName ];
57 window[ callbackName ] = function() {
58 responseContainer = arguments;
61 // Clean-up function (fires after converters)
62 jqXHR.always( function() {
64 // If previous value didn't exist - remove it
65 if ( overwritten === undefined ) {
66 jQuery( window ).removeProp( callbackName );
68 // Otherwise restore preexisting value
70 window[ callbackName ] = overwritten;
74 if ( s[ callbackName ] ) {
76 // Make sure that re-using the options doesn't screw things around
77 s.jsonpCallback = originalSettings.jsonpCallback;
79 // Save the callback name for future use
80 oldCallbacks.push( callbackName );
83 // Call if it was a function and we have a response
84 if ( responseContainer && typeof overwritten === "function" ) {
85 overwritten( responseContainer[ 0 ] );
88 responseContainer = overwritten = undefined;