10 ], function( jQuery, rnotwhite, nonce, rquery ) {
14 rts = /([?&])_=[^&]*/,
15 rheaders = /^(.*?):[ \t]*([^\r\n]*)$/mg,
16 // #7653, #8125, #8152: local protocol detection
17 rlocalProtocol = /^(?:about|app|app-storage|.+-extension|file|res|widget):$/,
18 rnoContent = /^(?:GET|HEAD)$/,
20 rurl = /^([\w.+-]+:)(?:\/\/(?:[^\/?#]*@|)([^\/?#:]*)(?::(\d+)|)|)/,
23 * 1) They are useful to introduce custom dataTypes (see ajax/jsonp.js for an example)
24 * 2) These are called:
25 * - BEFORE asking for a transport
26 * - AFTER param serialization (s.data is a string if s.processData is true)
27 * 3) key is the dataType
28 * 4) the catchall symbol "*" can be used
29 * 5) execution will start with transport dataType and THEN continue down to "*" if needed
33 /* Transports bindings
34 * 1) key is the dataType
35 * 2) the catchall symbol "*" can be used
36 * 3) selection will start with transport dataType and THEN go to "*" if needed
40 // Avoid comment-prolog char sequence (#10098); must appease lint and evade compression
41 allTypes = "*/".concat( "*" ),
44 ajaxLocation = window.location.href,
46 // Segment location into parts
47 ajaxLocParts = rurl.exec( ajaxLocation.toLowerCase() ) || [];
49 // Base "constructor" for jQuery.ajaxPrefilter and jQuery.ajaxTransport
50 function addToPrefiltersOrTransports( structure ) {
52 // dataTypeExpression is optional and defaults to "*"
53 return function( dataTypeExpression, func ) {
55 if ( typeof dataTypeExpression !== "string" ) {
56 func = dataTypeExpression;
57 dataTypeExpression = "*";
62 dataTypes = dataTypeExpression.toLowerCase().match( rnotwhite ) || [];
64 if ( jQuery.isFunction( func ) ) {
65 // For each dataType in the dataTypeExpression
66 while ( (dataType = dataTypes[i++]) ) {
67 // Prepend if requested
68 if ( dataType[0] === "+" ) {
69 dataType = dataType.slice( 1 ) || "*";
70 (structure[ dataType ] = structure[ dataType ] || []).unshift( func );
74 (structure[ dataType ] = structure[ dataType ] || []).push( func );
81 // Base inspection function for prefilters and transports
82 function inspectPrefiltersOrTransports( structure, options, originalOptions, jqXHR ) {
85 seekingTransport = ( structure === transports );
87 function inspect( dataType ) {
89 inspected[ dataType ] = true;
90 jQuery.each( structure[ dataType ] || [], function( _, prefilterOrFactory ) {
91 var dataTypeOrTransport = prefilterOrFactory( options, originalOptions, jqXHR );
92 if ( typeof dataTypeOrTransport === "string" && !seekingTransport && !inspected[ dataTypeOrTransport ] ) {
93 options.dataTypes.unshift( dataTypeOrTransport );
94 inspect( dataTypeOrTransport );
96 } else if ( seekingTransport ) {
97 return !( selected = dataTypeOrTransport );
103 return inspect( options.dataTypes[ 0 ] ) || !inspected[ "*" ] && inspect( "*" );
106 // A special extend for ajax options
107 // that takes "flat" options (not to be deep extended)
109 function ajaxExtend( target, src ) {
111 flatOptions = jQuery.ajaxSettings.flatOptions || {};
114 if ( src[ key ] !== undefined ) {
115 ( flatOptions[ key ] ? target : ( deep || (deep = {}) ) )[ key ] = src[ key ];
119 jQuery.extend( true, target, deep );
125 /* Handles responses to an ajax request:
126 * - finds the right dataType (mediates between content-type and expected dataType)
127 * - returns the corresponding response
129 function ajaxHandleResponses( s, jqXHR, responses ) {
131 var ct, type, finalDataType, firstDataType,
132 contents = s.contents,
133 dataTypes = s.dataTypes;
135 // Remove auto dataType and get content-type in the process
136 while ( dataTypes[ 0 ] === "*" ) {
138 if ( ct === undefined ) {
139 ct = s.mimeType || jqXHR.getResponseHeader("Content-Type");
143 // Check if we're dealing with a known content-type
145 for ( type in contents ) {
146 if ( contents[ type ] && contents[ type ].test( ct ) ) {
147 dataTypes.unshift( type );
153 // Check to see if we have a response for the expected dataType
154 if ( dataTypes[ 0 ] in responses ) {
155 finalDataType = dataTypes[ 0 ];
157 // Try convertible dataTypes
158 for ( type in responses ) {
159 if ( !dataTypes[ 0 ] || s.converters[ type + " " + dataTypes[0] ] ) {
160 finalDataType = type;
163 if ( !firstDataType ) {
164 firstDataType = type;
167 // Or just use first one
168 finalDataType = finalDataType || firstDataType;
171 // If we found a dataType
172 // We add the dataType to the list if needed
173 // and return the corresponding response
174 if ( finalDataType ) {
175 if ( finalDataType !== dataTypes[ 0 ] ) {
176 dataTypes.unshift( finalDataType );
178 return responses[ finalDataType ];
182 /* Chain conversions given the request and the original response
183 * Also sets the responseXXX fields on the jqXHR instance
185 function ajaxConvert( s, response, jqXHR, isSuccess ) {
186 var conv2, current, conv, tmp, prev,
188 // Work with a copy of dataTypes in case we need to modify it for conversion
189 dataTypes = s.dataTypes.slice();
191 // Create converters map with lowercased keys
192 if ( dataTypes[ 1 ] ) {
193 for ( conv in s.converters ) {
194 converters[ conv.toLowerCase() ] = s.converters[ conv ];
198 current = dataTypes.shift();
200 // Convert to each sequential dataType
203 if ( s.responseFields[ current ] ) {
204 jqXHR[ s.responseFields[ current ] ] = response;
207 // Apply the dataFilter if provided
208 if ( !prev && isSuccess && s.dataFilter ) {
209 response = s.dataFilter( response, s.dataType );
213 current = dataTypes.shift();
217 // There's only work to do if current dataType is non-auto
218 if ( current === "*" ) {
222 // Convert response if prev dataType is non-auto and differs from current
223 } else if ( prev !== "*" && prev !== current ) {
225 // Seek a direct converter
226 conv = converters[ prev + " " + current ] || converters[ "* " + current ];
228 // If none found, seek a pair
230 for ( conv2 in converters ) {
232 // If conv2 outputs current
233 tmp = conv2.split( " " );
234 if ( tmp[ 1 ] === current ) {
236 // If prev can be converted to accepted input
237 conv = converters[ prev + " " + tmp[ 0 ] ] ||
238 converters[ "* " + tmp[ 0 ] ];
240 // Condense equivalence converters
241 if ( conv === true ) {
242 conv = converters[ conv2 ];
244 // Otherwise, insert the intermediate dataType
245 } else if ( converters[ conv2 ] !== true ) {
247 dataTypes.unshift( tmp[ 1 ] );
255 // Apply converter (if not an equivalence)
256 if ( conv !== true ) {
258 // Unless errors are allowed to bubble, catch and return them
259 if ( conv && s[ "throws" ] ) {
260 response = conv( response );
263 response = conv( response );
265 return { state: "parsererror", error: conv ? e : "No conversion from " + prev + " to " + current };
273 return { state: "success", data: response };
278 // Counter for holding the number of active queries
281 // Last-Modified header cache for next request
288 isLocal: rlocalProtocol.test( ajaxLocParts[ 1 ] ),
292 contentType: "application/x-www-form-urlencoded; charset=UTF-8",
309 xml: "application/xml, text/xml",
310 json: "application/json, text/javascript"
321 text: "responseText",
326 // Keys separate source (or catchall "*") and destination types with a single space
329 // Convert anything to text
332 // Text to html (true = no transformation)
335 // Evaluate text as a json expression
336 "text json": jQuery.parseJSON,
339 "text xml": jQuery.parseXML
342 // For options that shouldn't be deep extended:
343 // you can add your own custom options here if
344 // and when you create one that shouldn't be
345 // deep extended (see ajaxExtend)
352 // Creates a full fledged settings object into target
353 // with both ajaxSettings and settings fields.
354 // If target is omitted, writes into ajaxSettings.
355 ajaxSetup: function( target, settings ) {
358 // Building a settings object
359 ajaxExtend( ajaxExtend( target, jQuery.ajaxSettings ), settings ) :
361 // Extending ajaxSettings
362 ajaxExtend( jQuery.ajaxSettings, target );
365 ajaxPrefilter: addToPrefiltersOrTransports( prefilters ),
366 ajaxTransport: addToPrefiltersOrTransports( transports ),
369 ajax: function( url, options ) {
371 // If url is an object, simulate pre-1.5 signature
372 if ( typeof url === "object" ) {
377 // Force options to be an object
378 options = options || {};
381 // URL without anti-cache param
384 responseHeadersString,
388 // Cross-domain detection vars
390 // To know if global events are to be dispatched
394 // Create the final options object
395 s = jQuery.ajaxSetup( {}, options ),
397 callbackContext = s.context || s,
398 // Context for global events is callbackContext if it is a DOM node or jQuery collection
399 globalEventContext = s.context && ( callbackContext.nodeType || callbackContext.jquery ) ?
400 jQuery( callbackContext ) :
403 deferred = jQuery.Deferred(),
404 completeDeferred = jQuery.Callbacks("once memory"),
405 // Status-dependent callbacks
406 statusCode = s.statusCode || {},
407 // Headers (they are sent all at once)
409 requestHeadersNames = {},
412 // Default abort message
413 strAbort = "canceled",
418 // Builds headers hashtable if needed
419 getResponseHeader: function( key ) {
422 if ( !responseHeaders ) {
423 responseHeaders = {};
424 while ( (match = rheaders.exec( responseHeadersString )) ) {
425 responseHeaders[ match[1].toLowerCase() ] = match[ 2 ];
428 match = responseHeaders[ key.toLowerCase() ];
430 return match == null ? null : match;
434 getAllResponseHeaders: function() {
435 return state === 2 ? responseHeadersString : null;
439 setRequestHeader: function( name, value ) {
440 var lname = name.toLowerCase();
442 name = requestHeadersNames[ lname ] = requestHeadersNames[ lname ] || name;
443 requestHeaders[ name ] = value;
448 // Overrides response content-type header
449 overrideMimeType: function( type ) {
456 // Status-dependent callbacks
457 statusCode: function( map ) {
461 for ( code in map ) {
462 // Lazy-add the new callback in a way that preserves old ones
463 statusCode[ code ] = [ statusCode[ code ], map[ code ] ];
466 // Execute the appropriate callbacks
467 jqXHR.always( map[ jqXHR.status ] );
473 // Cancel the request
474 abort: function( statusText ) {
475 var finalText = statusText || strAbort;
477 transport.abort( finalText );
479 done( 0, finalText );
485 deferred.promise( jqXHR ).complete = completeDeferred.add;
486 jqXHR.success = jqXHR.done;
487 jqXHR.error = jqXHR.fail;
489 // Remove hash character (#7531: and string promotion)
490 // Add protocol if not provided (prefilters might expect it)
491 // Handle falsy url in the settings object (#10093: consistency with old signature)
492 // We also use the url parameter if available
493 s.url = ( ( url || s.url || ajaxLocation ) + "" ).replace( rhash, "" )
494 .replace( rprotocol, ajaxLocParts[ 1 ] + "//" );
496 // Alias method option to type as per ticket #12004
497 s.type = options.method || options.type || s.method || s.type;
499 // Extract dataTypes list
500 s.dataTypes = jQuery.trim( s.dataType || "*" ).toLowerCase().match( rnotwhite ) || [ "" ];
502 // A cross-domain request is in order when we have a protocol:host:port mismatch
503 if ( s.crossDomain == null ) {
504 parts = rurl.exec( s.url.toLowerCase() );
505 s.crossDomain = !!( parts &&
506 ( parts[ 1 ] !== ajaxLocParts[ 1 ] || parts[ 2 ] !== ajaxLocParts[ 2 ] ||
507 ( parts[ 3 ] || ( parts[ 1 ] === "http:" ? "80" : "443" ) ) !==
508 ( ajaxLocParts[ 3 ] || ( ajaxLocParts[ 1 ] === "http:" ? "80" : "443" ) ) )
512 // Convert data if not already a string
513 if ( s.data && s.processData && typeof s.data !== "string" ) {
514 s.data = jQuery.param( s.data, s.traditional );
518 inspectPrefiltersOrTransports( prefilters, s, options, jqXHR );
520 // If request was aborted inside a prefilter, stop there
525 // We can fire global events as of now if asked to
526 // Don't fire events if jQuery.event is undefined in an AMD-usage scenario (#15118)
527 fireGlobals = jQuery.event && s.global;
529 // Watch for a new set of requests
530 if ( fireGlobals && jQuery.active++ === 0 ) {
531 jQuery.event.trigger("ajaxStart");
534 // Uppercase the type
535 s.type = s.type.toUpperCase();
537 // Determine if request has content
538 s.hasContent = !rnoContent.test( s.type );
540 // Save the URL in case we're toying with the If-Modified-Since
541 // and/or If-None-Match header later on
544 // More options handling for requests with no content
545 if ( !s.hasContent ) {
547 // If data is available, append data to url
549 cacheURL = ( s.url += ( rquery.test( cacheURL ) ? "&" : "?" ) + s.data );
550 // #9682: remove data so that it's not used in an eventual retry
554 // Add anti-cache in url if needed
555 if ( s.cache === false ) {
556 s.url = rts.test( cacheURL ) ?
558 // If there is already a '_' parameter, set its value
559 cacheURL.replace( rts, "$1_=" + nonce++ ) :
561 // Otherwise add one to the end
562 cacheURL + ( rquery.test( cacheURL ) ? "&" : "?" ) + "_=" + nonce++;
566 // Set the If-Modified-Since and/or If-None-Match header, if in ifModified mode.
567 if ( s.ifModified ) {
568 if ( jQuery.lastModified[ cacheURL ] ) {
569 jqXHR.setRequestHeader( "If-Modified-Since", jQuery.lastModified[ cacheURL ] );
571 if ( jQuery.etag[ cacheURL ] ) {
572 jqXHR.setRequestHeader( "If-None-Match", jQuery.etag[ cacheURL ] );
576 // Set the correct header, if data is being sent
577 if ( s.data && s.hasContent && s.contentType !== false || options.contentType ) {
578 jqXHR.setRequestHeader( "Content-Type", s.contentType );
581 // Set the Accepts header for the server, depending on the dataType
582 jqXHR.setRequestHeader(
584 s.dataTypes[ 0 ] && s.accepts[ s.dataTypes[0] ] ?
585 s.accepts[ s.dataTypes[0] ] + ( s.dataTypes[ 0 ] !== "*" ? ", " + allTypes + "; q=0.01" : "" ) :
589 // Check for headers option
590 for ( i in s.headers ) {
591 jqXHR.setRequestHeader( i, s.headers[ i ] );
594 // Allow custom headers/mimetypes and early abort
595 if ( s.beforeSend && ( s.beforeSend.call( callbackContext, jqXHR, s ) === false || state === 2 ) ) {
596 // Abort if not done already and return
597 return jqXHR.abort();
600 // Aborting is no longer a cancellation
603 // Install callbacks on deferreds
604 for ( i in { success: 1, error: 1, complete: 1 } ) {
605 jqXHR[ i ]( s[ i ] );
609 transport = inspectPrefiltersOrTransports( transports, s, options, jqXHR );
611 // If no transport, we auto-abort
613 done( -1, "No Transport" );
615 jqXHR.readyState = 1;
619 globalEventContext.trigger( "ajaxSend", [ jqXHR, s ] );
622 if ( s.async && s.timeout > 0 ) {
623 timeoutTimer = setTimeout(function() {
624 jqXHR.abort("timeout");
630 transport.send( requestHeaders, done );
632 // Propagate exception as error if not done
635 // Simply rethrow otherwise
642 // Callback for when everything is done
643 function done( status, nativeStatusText, responses, headers ) {
644 var isSuccess, success, error, response, modified,
645 statusText = nativeStatusText;
652 // State is "done" now
655 // Clear timeout if it exists
656 if ( timeoutTimer ) {
657 clearTimeout( timeoutTimer );
660 // Dereference transport for early garbage collection
661 // (no matter how long the jqXHR object will be used)
662 transport = undefined;
664 // Cache response headers
665 responseHeadersString = headers || "";
668 jqXHR.readyState = status > 0 ? 4 : 0;
670 // Determine if successful
671 isSuccess = status >= 200 && status < 300 || status === 304;
675 response = ajaxHandleResponses( s, jqXHR, responses );
678 // Convert no matter what (that way responseXXX fields are always set)
679 response = ajaxConvert( s, response, jqXHR, isSuccess );
681 // If successful, handle type chaining
684 // Set the If-Modified-Since and/or If-None-Match header, if in ifModified mode.
685 if ( s.ifModified ) {
686 modified = jqXHR.getResponseHeader("Last-Modified");
688 jQuery.lastModified[ cacheURL ] = modified;
690 modified = jqXHR.getResponseHeader("etag");
692 jQuery.etag[ cacheURL ] = modified;
697 if ( status === 204 || s.type === "HEAD" ) {
698 statusText = "nocontent";
701 } else if ( status === 304 ) {
702 statusText = "notmodified";
704 // If we have data, let's convert it
706 statusText = response.state;
707 success = response.data;
708 error = response.error;
712 // Extract error from statusText and normalize for non-aborts
714 if ( status || !statusText ) {
715 statusText = "error";
722 // Set data for the fake xhr object
723 jqXHR.status = status;
724 jqXHR.statusText = ( nativeStatusText || statusText ) + "";
728 deferred.resolveWith( callbackContext, [ success, statusText, jqXHR ] );
730 deferred.rejectWith( callbackContext, [ jqXHR, statusText, error ] );
733 // Status-dependent callbacks
734 jqXHR.statusCode( statusCode );
735 statusCode = undefined;
738 globalEventContext.trigger( isSuccess ? "ajaxSuccess" : "ajaxError",
739 [ jqXHR, s, isSuccess ? success : error ] );
743 completeDeferred.fireWith( callbackContext, [ jqXHR, statusText ] );
746 globalEventContext.trigger( "ajaxComplete", [ jqXHR, s ] );
747 // Handle the global AJAX counter
748 if ( !( --jQuery.active ) ) {
749 jQuery.event.trigger("ajaxStop");
757 getJSON: function( url, data, callback ) {
758 return jQuery.get( url, data, callback, "json" );
761 getScript: function( url, callback ) {
762 return jQuery.get( url, undefined, callback, "script" );
766 jQuery.each( [ "get", "post" ], function( i, method ) {
767 jQuery[ method ] = function( url, data, callback, type ) {
768 // Shift arguments if data argument was omitted
769 if ( jQuery.isFunction( data ) ) {
770 type = type || callback;