14 ], function( jQuery, document, rnotwhite, location, nonce, rquery ) {
18 rts = /([?&])_=[^&]*/,
19 rheaders = /^(.*?):[ \t]*([^\r\n]*)$/mg,
21 // #7653, #8125, #8152: local protocol detection
22 rlocalProtocol = /^(?:about|app|app-storage|.+-extension|file|res|widget):$/,
23 rnoContent = /^(?:GET|HEAD)$/,
27 * 1) They are useful to introduce custom dataTypes (see ajax/jsonp.js for an example)
28 * 2) These are called:
29 * - BEFORE asking for a transport
30 * - AFTER param serialization (s.data is a string if s.processData is true)
31 * 3) key is the dataType
32 * 4) the catchall symbol "*" can be used
33 * 5) execution will start with transport dataType and THEN continue down to "*" if needed
37 /* Transports bindings
38 * 1) key is the dataType
39 * 2) the catchall symbol "*" can be used
40 * 3) selection will start with transport dataType and THEN go to "*" if needed
44 // Avoid comment-prolog char sequence (#10098); must appease lint and evade compression
45 allTypes = "*/".concat( "*" ),
47 // Anchor tag for parsing the document origin
48 originAnchor = document.createElement( "a" );
49 originAnchor.href = location.href;
51 // Base "constructor" for jQuery.ajaxPrefilter and jQuery.ajaxTransport
52 function addToPrefiltersOrTransports( structure ) {
54 // dataTypeExpression is optional and defaults to "*"
55 return function( dataTypeExpression, func ) {
57 if ( typeof dataTypeExpression !== "string" ) {
58 func = dataTypeExpression;
59 dataTypeExpression = "*";
64 dataTypes = dataTypeExpression.toLowerCase().match( rnotwhite ) || [];
66 if ( jQuery.isFunction( func ) ) {
68 // For each dataType in the dataTypeExpression
69 while ( ( dataType = dataTypes[ i++ ] ) ) {
71 // Prepend if requested
72 if ( dataType[ 0 ] === "+" ) {
73 dataType = dataType.slice( 1 ) || "*";
74 ( structure[ dataType ] = structure[ dataType ] || [] ).unshift( func );
78 ( structure[ dataType ] = structure[ dataType ] || [] ).push( func );
85 // Base inspection function for prefilters and transports
86 function inspectPrefiltersOrTransports( structure, options, originalOptions, jqXHR ) {
89 seekingTransport = ( structure === transports );
91 function inspect( dataType ) {
93 inspected[ dataType ] = true;
94 jQuery.each( structure[ dataType ] || [], function( _, prefilterOrFactory ) {
95 var dataTypeOrTransport = prefilterOrFactory( options, originalOptions, jqXHR );
96 if ( typeof dataTypeOrTransport === "string" &&
97 !seekingTransport && !inspected[ dataTypeOrTransport ] ) {
99 options.dataTypes.unshift( dataTypeOrTransport );
100 inspect( dataTypeOrTransport );
102 } else if ( seekingTransport ) {
103 return !( selected = dataTypeOrTransport );
109 return inspect( options.dataTypes[ 0 ] ) || !inspected[ "*" ] && inspect( "*" );
112 // A special extend for ajax options
113 // that takes "flat" options (not to be deep extended)
115 function ajaxExtend( target, src ) {
117 flatOptions = jQuery.ajaxSettings.flatOptions || {};
120 if ( src[ key ] !== undefined ) {
121 ( flatOptions[ key ] ? target : ( deep || ( deep = {} ) ) )[ key ] = src[ key ];
125 jQuery.extend( true, target, deep );
131 /* Handles responses to an ajax request:
132 * - finds the right dataType (mediates between content-type and expected dataType)
133 * - returns the corresponding response
135 function ajaxHandleResponses( s, jqXHR, responses ) {
137 var ct, type, finalDataType, firstDataType,
138 contents = s.contents,
139 dataTypes = s.dataTypes;
141 // Remove auto dataType and get content-type in the process
142 while ( dataTypes[ 0 ] === "*" ) {
144 if ( ct === undefined ) {
145 ct = s.mimeType || jqXHR.getResponseHeader( "Content-Type" );
149 // Check if we're dealing with a known content-type
151 for ( type in contents ) {
152 if ( contents[ type ] && contents[ type ].test( ct ) ) {
153 dataTypes.unshift( type );
159 // Check to see if we have a response for the expected dataType
160 if ( dataTypes[ 0 ] in responses ) {
161 finalDataType = dataTypes[ 0 ];
164 // Try convertible dataTypes
165 for ( type in responses ) {
166 if ( !dataTypes[ 0 ] || s.converters[ type + " " + dataTypes[ 0 ] ] ) {
167 finalDataType = type;
170 if ( !firstDataType ) {
171 firstDataType = type;
175 // Or just use first one
176 finalDataType = finalDataType || firstDataType;
179 // If we found a dataType
180 // We add the dataType to the list if needed
181 // and return the corresponding response
182 if ( finalDataType ) {
183 if ( finalDataType !== dataTypes[ 0 ] ) {
184 dataTypes.unshift( finalDataType );
186 return responses[ finalDataType ];
190 /* Chain conversions given the request and the original response
191 * Also sets the responseXXX fields on the jqXHR instance
193 function ajaxConvert( s, response, jqXHR, isSuccess ) {
194 var conv2, current, conv, tmp, prev,
197 // Work with a copy of dataTypes in case we need to modify it for conversion
198 dataTypes = s.dataTypes.slice();
200 // Create converters map with lowercased keys
201 if ( dataTypes[ 1 ] ) {
202 for ( conv in s.converters ) {
203 converters[ conv.toLowerCase() ] = s.converters[ conv ];
207 current = dataTypes.shift();
209 // Convert to each sequential dataType
212 if ( s.responseFields[ current ] ) {
213 jqXHR[ s.responseFields[ current ] ] = response;
216 // Apply the dataFilter if provided
217 if ( !prev && isSuccess && s.dataFilter ) {
218 response = s.dataFilter( response, s.dataType );
222 current = dataTypes.shift();
226 // There's only work to do if current dataType is non-auto
227 if ( current === "*" ) {
231 // Convert response if prev dataType is non-auto and differs from current
232 } else if ( prev !== "*" && prev !== current ) {
234 // Seek a direct converter
235 conv = converters[ prev + " " + current ] || converters[ "* " + current ];
237 // If none found, seek a pair
239 for ( conv2 in converters ) {
241 // If conv2 outputs current
242 tmp = conv2.split( " " );
243 if ( tmp[ 1 ] === current ) {
245 // If prev can be converted to accepted input
246 conv = converters[ prev + " " + tmp[ 0 ] ] ||
247 converters[ "* " + tmp[ 0 ] ];
250 // Condense equivalence converters
251 if ( conv === true ) {
252 conv = converters[ conv2 ];
254 // Otherwise, insert the intermediate dataType
255 } else if ( converters[ conv2 ] !== true ) {
257 dataTypes.unshift( tmp[ 1 ] );
265 // Apply converter (if not an equivalence)
266 if ( conv !== true ) {
268 // Unless errors are allowed to bubble, catch and return them
269 if ( conv && s.throws ) {
270 response = conv( response );
273 response = conv( response );
276 state: "parsererror",
277 error: conv ? e : "No conversion from " + prev + " to " + current
286 return { state: "success", data: response };
291 // Counter for holding the number of active queries
294 // Last-Modified header cache for next request
301 isLocal: rlocalProtocol.test( location.protocol ),
305 contentType: "application/x-www-form-urlencoded; charset=UTF-8",
322 xml: "application/xml, text/xml",
323 json: "application/json, text/javascript"
334 text: "responseText",
339 // Keys separate source (or catchall "*") and destination types with a single space
342 // Convert anything to text
345 // Text to html (true = no transformation)
348 // Evaluate text as a json expression
349 "text json": jQuery.parseJSON,
352 "text xml": jQuery.parseXML
355 // For options that shouldn't be deep extended:
356 // you can add your own custom options here if
357 // and when you create one that shouldn't be
358 // deep extended (see ajaxExtend)
365 // Creates a full fledged settings object into target
366 // with both ajaxSettings and settings fields.
367 // If target is omitted, writes into ajaxSettings.
368 ajaxSetup: function( target, settings ) {
371 // Building a settings object
372 ajaxExtend( ajaxExtend( target, jQuery.ajaxSettings ), settings ) :
374 // Extending ajaxSettings
375 ajaxExtend( jQuery.ajaxSettings, target );
378 ajaxPrefilter: addToPrefiltersOrTransports( prefilters ),
379 ajaxTransport: addToPrefiltersOrTransports( transports ),
382 ajax: function( url, options ) {
384 // If url is an object, simulate pre-1.5 signature
385 if ( typeof url === "object" ) {
390 // Force options to be an object
391 options = options || {};
395 // URL without anti-cache param
399 responseHeadersString,
408 // To know if global events are to be dispatched
414 // Create the final options object
415 s = jQuery.ajaxSetup( {}, options ),
418 callbackContext = s.context || s,
420 // Context for global events is callbackContext if it is a DOM node or jQuery collection
421 globalEventContext = s.context &&
422 ( callbackContext.nodeType || callbackContext.jquery ) ?
423 jQuery( callbackContext ) :
427 deferred = jQuery.Deferred(),
428 completeDeferred = jQuery.Callbacks( "once memory" ),
430 // Status-dependent callbacks
431 statusCode = s.statusCode || {},
433 // Headers (they are sent all at once)
435 requestHeadersNames = {},
440 // Default abort message
441 strAbort = "canceled",
447 // Builds headers hashtable if needed
448 getResponseHeader: function( key ) {
451 if ( !responseHeaders ) {
452 responseHeaders = {};
453 while ( ( match = rheaders.exec( responseHeadersString ) ) ) {
454 responseHeaders[ match[ 1 ].toLowerCase() ] = match[ 2 ];
457 match = responseHeaders[ key.toLowerCase() ];
459 return match == null ? null : match;
463 getAllResponseHeaders: function() {
464 return state === 2 ? responseHeadersString : null;
468 setRequestHeader: function( name, value ) {
469 var lname = name.toLowerCase();
471 name = requestHeadersNames[ lname ] = requestHeadersNames[ lname ] || name;
472 requestHeaders[ name ] = value;
477 // Overrides response content-type header
478 overrideMimeType: function( type ) {
485 // Status-dependent callbacks
486 statusCode: function( map ) {
490 for ( code in map ) {
492 // Lazy-add the new callback in a way that preserves old ones
493 statusCode[ code ] = [ statusCode[ code ], map[ code ] ];
497 // Execute the appropriate callbacks
498 jqXHR.always( map[ jqXHR.status ] );
504 // Cancel the request
505 abort: function( statusText ) {
506 var finalText = statusText || strAbort;
508 transport.abort( finalText );
510 done( 0, finalText );
516 deferred.promise( jqXHR ).complete = completeDeferred.add;
517 jqXHR.success = jqXHR.done;
518 jqXHR.error = jqXHR.fail;
520 // Remove hash character (#7531: and string promotion)
521 // Add protocol if not provided (prefilters might expect it)
522 // Handle falsy url in the settings object (#10093: consistency with old signature)
523 // We also use the url parameter if available
524 s.url = ( ( url || s.url || location.href ) + "" ).replace( rhash, "" )
525 .replace( rprotocol, location.protocol + "//" );
527 // Alias method option to type as per ticket #12004
528 s.type = options.method || options.type || s.method || s.type;
530 // Extract dataTypes list
531 s.dataTypes = jQuery.trim( s.dataType || "*" ).toLowerCase().match( rnotwhite ) || [ "" ];
533 // A cross-domain request is in order when the origin doesn't match the current origin.
534 if ( s.crossDomain == null ) {
535 urlAnchor = document.createElement( "a" );
538 // IE throws exception if url is malformed, e.g. http://example.com:80x/
540 urlAnchor.href = s.url;
543 // Anchor's host property isn't correctly set when s.url is relative
544 urlAnchor.href = urlAnchor.href;
545 s.crossDomain = originAnchor.protocol + "//" + originAnchor.host !==
546 urlAnchor.protocol + "//" + urlAnchor.host;
549 // If there is an error parsing the URL, assume it is crossDomain,
550 // it can be rejected by the transport if it is invalid
551 s.crossDomain = true;
555 // Convert data if not already a string
556 if ( s.data && s.processData && typeof s.data !== "string" ) {
557 s.data = jQuery.param( s.data, s.traditional );
561 inspectPrefiltersOrTransports( prefilters, s, options, jqXHR );
563 // If request was aborted inside a prefilter, stop there
568 // We can fire global events as of now if asked to
569 // Don't fire events if jQuery.event is undefined in an AMD-usage scenario (#15118)
570 fireGlobals = jQuery.event && s.global;
572 // Watch for a new set of requests
573 if ( fireGlobals && jQuery.active++ === 0 ) {
574 jQuery.event.trigger( "ajaxStart" );
577 // Uppercase the type
578 s.type = s.type.toUpperCase();
580 // Determine if request has content
581 s.hasContent = !rnoContent.test( s.type );
583 // Save the URL in case we're toying with the If-Modified-Since
584 // and/or If-None-Match header later on
587 // More options handling for requests with no content
588 if ( !s.hasContent ) {
590 // If data is available, append data to url
592 cacheURL = ( s.url += ( rquery.test( cacheURL ) ? "&" : "?" ) + s.data );
594 // #9682: remove data so that it's not used in an eventual retry
598 // Add anti-cache in url if needed
599 if ( s.cache === false ) {
600 s.url = rts.test( cacheURL ) ?
602 // If there is already a '_' parameter, set its value
603 cacheURL.replace( rts, "$1_=" + nonce++ ) :
605 // Otherwise add one to the end
606 cacheURL + ( rquery.test( cacheURL ) ? "&" : "?" ) + "_=" + nonce++;
610 // Set the If-Modified-Since and/or If-None-Match header, if in ifModified mode.
611 if ( s.ifModified ) {
612 if ( jQuery.lastModified[ cacheURL ] ) {
613 jqXHR.setRequestHeader( "If-Modified-Since", jQuery.lastModified[ cacheURL ] );
615 if ( jQuery.etag[ cacheURL ] ) {
616 jqXHR.setRequestHeader( "If-None-Match", jQuery.etag[ cacheURL ] );
620 // Set the correct header, if data is being sent
621 if ( s.data && s.hasContent && s.contentType !== false || options.contentType ) {
622 jqXHR.setRequestHeader( "Content-Type", s.contentType );
625 // Set the Accepts header for the server, depending on the dataType
626 jqXHR.setRequestHeader(
628 s.dataTypes[ 0 ] && s.accepts[ s.dataTypes[ 0 ] ] ?
629 s.accepts[ s.dataTypes[ 0 ] ] +
630 ( s.dataTypes[ 0 ] !== "*" ? ", " + allTypes + "; q=0.01" : "" ) :
634 // Check for headers option
635 for ( i in s.headers ) {
636 jqXHR.setRequestHeader( i, s.headers[ i ] );
639 // Allow custom headers/mimetypes and early abort
641 ( s.beforeSend.call( callbackContext, jqXHR, s ) === false || state === 2 ) ) {
643 // Abort if not done already and return
644 return jqXHR.abort();
647 // Aborting is no longer a cancellation
650 // Install callbacks on deferreds
651 for ( i in { success: 1, error: 1, complete: 1 } ) {
652 jqXHR[ i ]( s[ i ] );
656 transport = inspectPrefiltersOrTransports( transports, s, options, jqXHR );
658 // If no transport, we auto-abort
660 done( -1, "No Transport" );
662 jqXHR.readyState = 1;
666 globalEventContext.trigger( "ajaxSend", [ jqXHR, s ] );
669 // If request was aborted inside ajaxSend, stop there
675 if ( s.async && s.timeout > 0 ) {
676 timeoutTimer = window.setTimeout( function() {
677 jqXHR.abort( "timeout" );
683 transport.send( requestHeaders, done );
686 // Propagate exception as error if not done
690 // Simply rethrow otherwise
697 // Callback for when everything is done
698 function done( status, nativeStatusText, responses, headers ) {
699 var isSuccess, success, error, response, modified,
700 statusText = nativeStatusText;
707 // State is "done" now
710 // Clear timeout if it exists
711 if ( timeoutTimer ) {
712 window.clearTimeout( timeoutTimer );
715 // Dereference transport for early garbage collection
716 // (no matter how long the jqXHR object will be used)
717 transport = undefined;
719 // Cache response headers
720 responseHeadersString = headers || "";
723 jqXHR.readyState = status > 0 ? 4 : 0;
725 // Determine if successful
726 isSuccess = status >= 200 && status < 300 || status === 304;
730 response = ajaxHandleResponses( s, jqXHR, responses );
733 // Convert no matter what (that way responseXXX fields are always set)
734 response = ajaxConvert( s, response, jqXHR, isSuccess );
736 // If successful, handle type chaining
739 // Set the If-Modified-Since and/or If-None-Match header, if in ifModified mode.
740 if ( s.ifModified ) {
741 modified = jqXHR.getResponseHeader( "Last-Modified" );
743 jQuery.lastModified[ cacheURL ] = modified;
745 modified = jqXHR.getResponseHeader( "etag" );
747 jQuery.etag[ cacheURL ] = modified;
752 if ( status === 204 || s.type === "HEAD" ) {
753 statusText = "nocontent";
756 } else if ( status === 304 ) {
757 statusText = "notmodified";
759 // If we have data, let's convert it
761 statusText = response.state;
762 success = response.data;
763 error = response.error;
768 // Extract error from statusText and normalize for non-aborts
770 if ( status || !statusText ) {
771 statusText = "error";
778 // Set data for the fake xhr object
779 jqXHR.status = status;
780 jqXHR.statusText = ( nativeStatusText || statusText ) + "";
784 deferred.resolveWith( callbackContext, [ success, statusText, jqXHR ] );
786 deferred.rejectWith( callbackContext, [ jqXHR, statusText, error ] );
789 // Status-dependent callbacks
790 jqXHR.statusCode( statusCode );
791 statusCode = undefined;
794 globalEventContext.trigger( isSuccess ? "ajaxSuccess" : "ajaxError",
795 [ jqXHR, s, isSuccess ? success : error ] );
799 completeDeferred.fireWith( callbackContext, [ jqXHR, statusText ] );
802 globalEventContext.trigger( "ajaxComplete", [ jqXHR, s ] );
804 // Handle the global AJAX counter
805 if ( !( --jQuery.active ) ) {
806 jQuery.event.trigger( "ajaxStop" );
814 getJSON: function( url, data, callback ) {
815 return jQuery.get( url, data, callback, "json" );
818 getScript: function( url, callback ) {
819 return jQuery.get( url, undefined, callback, "script" );
823 jQuery.each( [ "get", "post" ], function( i, method ) {
824 jQuery[ method ] = function( url, data, callback, type ) {
826 // Shift arguments if data argument was omitted
827 if ( jQuery.isFunction( data ) ) {
828 type = type || callback;
833 // The url can be an options object (which then must have .url)
834 return jQuery.ajax( jQuery.extend( {
840 }, jQuery.isPlainObject( url ) && url ) );