12 ], function( jQuery, document, rnotwhite, location, nonce, rquery ) {
16 rts = /([?&])_=[^&]*/,
17 rheaders = /^(.*?):[ \t]*([^\r\n]*)$/mg,
18 // #7653, #8125, #8152: local protocol detection
19 rlocalProtocol = /^(?:about|app|app-storage|.+-extension|file|res|widget):$/,
20 rnoContent = /^(?:GET|HEAD)$/,
24 * 1) They are useful to introduce custom dataTypes (see ajax/jsonp.js for an example)
25 * 2) These are called:
26 * - BEFORE asking for a transport
27 * - AFTER param serialization (s.data is a string if s.processData is true)
28 * 3) key is the dataType
29 * 4) the catchall symbol "*" can be used
30 * 5) execution will start with transport dataType and THEN continue down to "*" if needed
34 /* Transports bindings
35 * 1) key is the dataType
36 * 2) the catchall symbol "*" can be used
37 * 3) selection will start with transport dataType and THEN go to "*" if needed
41 // Avoid comment-prolog char sequence (#10098); must appease lint and evade compression
42 allTypes = "*/".concat( "*" ),
44 // Anchor tag for parsing the document origin
45 originAnchor = document.createElement( "a" );
46 originAnchor.href = location.href;
48 // Base "constructor" for jQuery.ajaxPrefilter and jQuery.ajaxTransport
49 function addToPrefiltersOrTransports( structure ) {
51 // dataTypeExpression is optional and defaults to "*"
52 return function( dataTypeExpression, func ) {
54 if ( typeof dataTypeExpression !== "string" ) {
55 func = dataTypeExpression;
56 dataTypeExpression = "*";
61 dataTypes = dataTypeExpression.toLowerCase().match( rnotwhite ) || [];
63 if ( jQuery.isFunction( func ) ) {
64 // For each dataType in the dataTypeExpression
65 while ( (dataType = dataTypes[i++]) ) {
66 // Prepend if requested
67 if ( dataType[0] === "+" ) {
68 dataType = dataType.slice( 1 ) || "*";
69 (structure[ dataType ] = structure[ dataType ] || []).unshift( func );
73 (structure[ dataType ] = structure[ dataType ] || []).push( func );
80 // Base inspection function for prefilters and transports
81 function inspectPrefiltersOrTransports( structure, options, originalOptions, jqXHR ) {
84 seekingTransport = ( structure === transports );
86 function inspect( dataType ) {
88 inspected[ dataType ] = true;
89 jQuery.each( structure[ dataType ] || [], function( _, prefilterOrFactory ) {
90 var dataTypeOrTransport = prefilterOrFactory( options, originalOptions, jqXHR );
91 if ( typeof dataTypeOrTransport === "string" &&
92 !seekingTransport && !inspected[ dataTypeOrTransport ] ) {
94 options.dataTypes.unshift( dataTypeOrTransport );
95 inspect( dataTypeOrTransport );
97 } else if ( seekingTransport ) {
98 return !( selected = dataTypeOrTransport );
104 return inspect( options.dataTypes[ 0 ] ) || !inspected[ "*" ] && inspect( "*" );
107 // A special extend for ajax options
108 // that takes "flat" options (not to be deep extended)
110 function ajaxExtend( target, src ) {
112 flatOptions = jQuery.ajaxSettings.flatOptions || {};
115 if ( src[ key ] !== undefined ) {
116 ( flatOptions[ key ] ? target : ( deep || (deep = {}) ) )[ key ] = src[ key ];
120 jQuery.extend( true, target, deep );
126 /* Handles responses to an ajax request:
127 * - finds the right dataType (mediates between content-type and expected dataType)
128 * - returns the corresponding response
130 function ajaxHandleResponses( s, jqXHR, responses ) {
132 var ct, type, finalDataType, firstDataType,
133 contents = s.contents,
134 dataTypes = s.dataTypes;
136 // Remove auto dataType and get content-type in the process
137 while ( dataTypes[ 0 ] === "*" ) {
139 if ( ct === undefined ) {
140 ct = s.mimeType || jqXHR.getResponseHeader("Content-Type");
144 // Check if we're dealing with a known content-type
146 for ( type in contents ) {
147 if ( contents[ type ] && contents[ type ].test( ct ) ) {
148 dataTypes.unshift( type );
154 // Check to see if we have a response for the expected dataType
155 if ( dataTypes[ 0 ] in responses ) {
156 finalDataType = dataTypes[ 0 ];
158 // Try convertible dataTypes
159 for ( type in responses ) {
160 if ( !dataTypes[ 0 ] || s.converters[ type + " " + dataTypes[0] ] ) {
161 finalDataType = type;
164 if ( !firstDataType ) {
165 firstDataType = type;
168 // Or just use first one
169 finalDataType = finalDataType || firstDataType;
172 // If we found a dataType
173 // We add the dataType to the list if needed
174 // and return the corresponding response
175 if ( finalDataType ) {
176 if ( finalDataType !== dataTypes[ 0 ] ) {
177 dataTypes.unshift( finalDataType );
179 return responses[ finalDataType ];
183 /* Chain conversions given the request and the original response
184 * Also sets the responseXXX fields on the jqXHR instance
186 function ajaxConvert( s, response, jqXHR, isSuccess ) {
187 var conv2, current, conv, tmp, prev,
189 // Work with a copy of dataTypes in case we need to modify it for conversion
190 dataTypes = s.dataTypes.slice();
192 // Create converters map with lowercased keys
193 if ( dataTypes[ 1 ] ) {
194 for ( conv in s.converters ) {
195 converters[ conv.toLowerCase() ] = s.converters[ conv ];
199 current = dataTypes.shift();
201 // Convert to each sequential dataType
204 if ( s.responseFields[ current ] ) {
205 jqXHR[ s.responseFields[ current ] ] = response;
208 // Apply the dataFilter if provided
209 if ( !prev && isSuccess && s.dataFilter ) {
210 response = s.dataFilter( response, s.dataType );
214 current = dataTypes.shift();
218 // There's only work to do if current dataType is non-auto
219 if ( current === "*" ) {
223 // Convert response if prev dataType is non-auto and differs from current
224 } else if ( prev !== "*" && prev !== current ) {
226 // Seek a direct converter
227 conv = converters[ prev + " " + current ] || converters[ "* " + current ];
229 // If none found, seek a pair
231 for ( conv2 in converters ) {
233 // If conv2 outputs current
234 tmp = conv2.split( " " );
235 if ( tmp[ 1 ] === current ) {
237 // If prev can be converted to accepted input
238 conv = converters[ prev + " " + tmp[ 0 ] ] ||
239 converters[ "* " + tmp[ 0 ] ];
241 // Condense equivalence converters
242 if ( conv === true ) {
243 conv = converters[ conv2 ];
245 // Otherwise, insert the intermediate dataType
246 } else if ( converters[ conv2 ] !== true ) {
248 dataTypes.unshift( tmp[ 1 ] );
256 // Apply converter (if not an equivalence)
257 if ( conv !== true ) {
259 // Unless errors are allowed to bubble, catch and return them
260 if ( conv && s[ "throws" ] ) {
261 response = conv( response );
264 response = conv( response );
267 state: "parsererror",
268 error: conv ? e : "No conversion from " + prev + " to " + current
277 return { state: "success", data: response };
282 // Counter for holding the number of active queries
285 // Last-Modified header cache for next request
292 isLocal: rlocalProtocol.test( location.protocol ),
296 contentType: "application/x-www-form-urlencoded; charset=UTF-8",
313 xml: "application/xml, text/xml",
314 json: "application/json, text/javascript"
325 text: "responseText",
330 // Keys separate source (or catchall "*") and destination types with a single space
333 // Convert anything to text
336 // Text to html (true = no transformation)
339 // Evaluate text as a json expression
340 "text json": jQuery.parseJSON,
343 "text xml": jQuery.parseXML
346 // For options that shouldn't be deep extended:
347 // you can add your own custom options here if
348 // and when you create one that shouldn't be
349 // deep extended (see ajaxExtend)
356 // Creates a full fledged settings object into target
357 // with both ajaxSettings and settings fields.
358 // If target is omitted, writes into ajaxSettings.
359 ajaxSetup: function( target, settings ) {
362 // Building a settings object
363 ajaxExtend( ajaxExtend( target, jQuery.ajaxSettings ), settings ) :
365 // Extending ajaxSettings
366 ajaxExtend( jQuery.ajaxSettings, target );
369 ajaxPrefilter: addToPrefiltersOrTransports( prefilters ),
370 ajaxTransport: addToPrefiltersOrTransports( transports ),
373 ajax: function( url, options ) {
375 // If url is an object, simulate pre-1.5 signature
376 if ( typeof url === "object" ) {
381 // Force options to be an object
382 options = options || {};
385 // URL without anti-cache param
388 responseHeadersString,
394 // To know if global events are to be dispatched
398 // Create the final options object
399 s = jQuery.ajaxSetup( {}, options ),
401 callbackContext = s.context || s,
402 // Context for global events is callbackContext if it is a DOM node or jQuery collection
403 globalEventContext = s.context &&
404 ( callbackContext.nodeType || callbackContext.jquery ) ?
405 jQuery( callbackContext ) :
408 deferred = jQuery.Deferred(),
409 completeDeferred = jQuery.Callbacks("once memory"),
410 // Status-dependent callbacks
411 statusCode = s.statusCode || {},
412 // Headers (they are sent all at once)
414 requestHeadersNames = {},
417 // Default abort message
418 strAbort = "canceled",
423 // Builds headers hashtable if needed
424 getResponseHeader: function( key ) {
427 if ( !responseHeaders ) {
428 responseHeaders = {};
429 while ( (match = rheaders.exec( responseHeadersString )) ) {
430 responseHeaders[ match[1].toLowerCase() ] = match[ 2 ];
433 match = responseHeaders[ key.toLowerCase() ];
435 return match == null ? null : match;
439 getAllResponseHeaders: function() {
440 return state === 2 ? responseHeadersString : null;
444 setRequestHeader: function( name, value ) {
445 var lname = name.toLowerCase();
447 name = requestHeadersNames[ lname ] = requestHeadersNames[ lname ] || name;
448 requestHeaders[ name ] = value;
453 // Overrides response content-type header
454 overrideMimeType: function( type ) {
461 // Status-dependent callbacks
462 statusCode: function( map ) {
466 for ( code in map ) {
467 // Lazy-add the new callback in a way that preserves old ones
468 statusCode[ code ] = [ statusCode[ code ], map[ code ] ];
471 // Execute the appropriate callbacks
472 jqXHR.always( map[ jqXHR.status ] );
478 // Cancel the request
479 abort: function( statusText ) {
480 var finalText = statusText || strAbort;
482 transport.abort( finalText );
484 done( 0, finalText );
490 deferred.promise( jqXHR ).complete = completeDeferred.add;
491 jqXHR.success = jqXHR.done;
492 jqXHR.error = jqXHR.fail;
494 // Remove hash character (#7531: and string promotion)
495 // Add protocol if not provided (prefilters might expect it)
496 // Handle falsy url in the settings object (#10093: consistency with old signature)
497 // We also use the url parameter if available
498 s.url = ( ( url || s.url || location.href ) + "" ).replace( rhash, "" )
499 .replace( rprotocol, location.protocol + "//" );
501 // Alias method option to type as per ticket #12004
502 s.type = options.method || options.type || s.method || s.type;
504 // Extract dataTypes list
505 s.dataTypes = jQuery.trim( s.dataType || "*" ).toLowerCase().match( rnotwhite ) || [ "" ];
507 // A cross-domain request is in order when the origin doesn't match the current origin.
508 if ( s.crossDomain == null ) {
509 urlAnchor = document.createElement( "a" );
512 // IE throws exception if url is malformed, e.g. http://example.com:80x/
514 urlAnchor.href = s.url;
516 // Anchor's host property isn't correctly set when s.url is relative
517 urlAnchor.href = urlAnchor.href;
518 s.crossDomain = originAnchor.protocol + "//" + originAnchor.host !==
519 urlAnchor.protocol + "//" + urlAnchor.host;
521 // If there is an error parsing the URL, assume it is crossDomain,
522 // it can be rejected by the transport if it is invalid
523 s.crossDomain = true;
527 // Convert data if not already a string
528 if ( s.data && s.processData && typeof s.data !== "string" ) {
529 s.data = jQuery.param( s.data, s.traditional );
533 inspectPrefiltersOrTransports( prefilters, s, options, jqXHR );
535 // If request was aborted inside a prefilter, stop there
540 // We can fire global events as of now if asked to
541 // Don't fire events if jQuery.event is undefined in an AMD-usage scenario (#15118)
542 fireGlobals = jQuery.event && s.global;
544 // Watch for a new set of requests
545 if ( fireGlobals && jQuery.active++ === 0 ) {
546 jQuery.event.trigger("ajaxStart");
549 // Uppercase the type
550 s.type = s.type.toUpperCase();
552 // Determine if request has content
553 s.hasContent = !rnoContent.test( s.type );
555 // Save the URL in case we're toying with the If-Modified-Since
556 // and/or If-None-Match header later on
559 // More options handling for requests with no content
560 if ( !s.hasContent ) {
562 // If data is available, append data to url
564 cacheURL = ( s.url += ( rquery.test( cacheURL ) ? "&" : "?" ) + s.data );
565 // #9682: remove data so that it's not used in an eventual retry
569 // Add anti-cache in url if needed
570 if ( s.cache === false ) {
571 s.url = rts.test( cacheURL ) ?
573 // If there is already a '_' parameter, set its value
574 cacheURL.replace( rts, "$1_=" + nonce++ ) :
576 // Otherwise add one to the end
577 cacheURL + ( rquery.test( cacheURL ) ? "&" : "?" ) + "_=" + nonce++;
581 // Set the If-Modified-Since and/or If-None-Match header, if in ifModified mode.
582 if ( s.ifModified ) {
583 if ( jQuery.lastModified[ cacheURL ] ) {
584 jqXHR.setRequestHeader( "If-Modified-Since", jQuery.lastModified[ cacheURL ] );
586 if ( jQuery.etag[ cacheURL ] ) {
587 jqXHR.setRequestHeader( "If-None-Match", jQuery.etag[ cacheURL ] );
591 // Set the correct header, if data is being sent
592 if ( s.data && s.hasContent && s.contentType !== false || options.contentType ) {
593 jqXHR.setRequestHeader( "Content-Type", s.contentType );
596 // Set the Accepts header for the server, depending on the dataType
597 jqXHR.setRequestHeader(
599 s.dataTypes[ 0 ] && s.accepts[ s.dataTypes[0] ] ?
600 s.accepts[ s.dataTypes[0] ] +
601 ( s.dataTypes[ 0 ] !== "*" ? ", " + allTypes + "; q=0.01" : "" ) :
605 // Check for headers option
606 for ( i in s.headers ) {
607 jqXHR.setRequestHeader( i, s.headers[ i ] );
610 // Allow custom headers/mimetypes and early abort
612 ( s.beforeSend.call( callbackContext, jqXHR, s ) === false || state === 2 ) ) {
614 // Abort if not done already and return
615 return jqXHR.abort();
618 // Aborting is no longer a cancellation
621 // Install callbacks on deferreds
622 for ( i in { success: 1, error: 1, complete: 1 } ) {
623 jqXHR[ i ]( s[ i ] );
627 transport = inspectPrefiltersOrTransports( transports, s, options, jqXHR );
629 // If no transport, we auto-abort
631 done( -1, "No Transport" );
633 jqXHR.readyState = 1;
637 globalEventContext.trigger( "ajaxSend", [ jqXHR, s ] );
640 // If request was aborted inside ajaxSend, stop there
646 if ( s.async && s.timeout > 0 ) {
647 timeoutTimer = setTimeout(function() {
648 jqXHR.abort("timeout");
654 transport.send( requestHeaders, done );
656 // Propagate exception as error if not done
659 // Simply rethrow otherwise
666 // Callback for when everything is done
667 function done( status, nativeStatusText, responses, headers ) {
668 var isSuccess, success, error, response, modified,
669 statusText = nativeStatusText;
676 // State is "done" now
679 // Clear timeout if it exists
680 if ( timeoutTimer ) {
681 clearTimeout( timeoutTimer );
684 // Dereference transport for early garbage collection
685 // (no matter how long the jqXHR object will be used)
686 transport = undefined;
688 // Cache response headers
689 responseHeadersString = headers || "";
692 jqXHR.readyState = status > 0 ? 4 : 0;
694 // Determine if successful
695 isSuccess = status >= 200 && status < 300 || status === 304;
699 response = ajaxHandleResponses( s, jqXHR, responses );
702 // Convert no matter what (that way responseXXX fields are always set)
703 response = ajaxConvert( s, response, jqXHR, isSuccess );
705 // If successful, handle type chaining
708 // Set the If-Modified-Since and/or If-None-Match header, if in ifModified mode.
709 if ( s.ifModified ) {
710 modified = jqXHR.getResponseHeader("Last-Modified");
712 jQuery.lastModified[ cacheURL ] = modified;
714 modified = jqXHR.getResponseHeader("etag");
716 jQuery.etag[ cacheURL ] = modified;
721 if ( status === 204 || s.type === "HEAD" ) {
722 statusText = "nocontent";
725 } else if ( status === 304 ) {
726 statusText = "notmodified";
728 // If we have data, let's convert it
730 statusText = response.state;
731 success = response.data;
732 error = response.error;
736 // Extract error from statusText and normalize for non-aborts
738 if ( status || !statusText ) {
739 statusText = "error";
746 // Set data for the fake xhr object
747 jqXHR.status = status;
748 jqXHR.statusText = ( nativeStatusText || statusText ) + "";
752 deferred.resolveWith( callbackContext, [ success, statusText, jqXHR ] );
754 deferred.rejectWith( callbackContext, [ jqXHR, statusText, error ] );
757 // Status-dependent callbacks
758 jqXHR.statusCode( statusCode );
759 statusCode = undefined;
762 globalEventContext.trigger( isSuccess ? "ajaxSuccess" : "ajaxError",
763 [ jqXHR, s, isSuccess ? success : error ] );
767 completeDeferred.fireWith( callbackContext, [ jqXHR, statusText ] );
770 globalEventContext.trigger( "ajaxComplete", [ jqXHR, s ] );
771 // Handle the global AJAX counter
772 if ( !( --jQuery.active ) ) {
773 jQuery.event.trigger("ajaxStop");
781 getJSON: function( url, data, callback ) {
782 return jQuery.get( url, data, callback, "json" );
785 getScript: function( url, callback ) {
786 return jQuery.get( url, undefined, callback, "script" );
790 jQuery.each( [ "get", "post" ], function( i, method ) {
791 jQuery[ method ] = function( url, data, callback, type ) {
792 // Shift arguments if data argument was omitted
793 if ( jQuery.isFunction( data ) ) {
794 type = type || callback;
799 // The url can be an options object (which then must have .url)
800 return jQuery.ajax( jQuery.extend({
806 }, jQuery.isPlainObject( url ) && url ) );