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)$/,
22 * 1) They are useful to introduce custom dataTypes (see ajax/jsonp.js for an example)
23 * 2) These are called:
24 * - BEFORE asking for a transport
25 * - AFTER param serialization (s.data is a string if s.processData is true)
26 * 3) key is the dataType
27 * 4) the catchall symbol "*" can be used
28 * 5) execution will start with transport dataType and THEN continue down to "*" if needed
32 /* Transports bindings
33 * 1) key is the dataType
34 * 2) the catchall symbol "*" can be used
35 * 3) selection will start with transport dataType and THEN go to "*" if needed
39 // Avoid comment-prolog char sequence (#10098); must appease lint and evade compression
40 allTypes = "*/".concat( "*" ),
42 // Anchor tag for parsing the document origin
43 originAnchor = document.createElement( "a" );
45 originAnchor.href = location.href;
47 // Base "constructor" for jQuery.ajaxPrefilter and jQuery.ajaxTransport
48 function addToPrefiltersOrTransports( structure ) {
50 // dataTypeExpression is optional and defaults to "*"
51 return function( dataTypeExpression, func ) {
53 if ( typeof dataTypeExpression !== "string" ) {
54 func = dataTypeExpression;
55 dataTypeExpression = "*";
60 dataTypes = dataTypeExpression.toLowerCase().match( rnotwhite ) || [];
62 if ( jQuery.isFunction( func ) ) {
63 // For each dataType in the dataTypeExpression
64 while ( (dataType = dataTypes[i++]) ) {
65 // Prepend if requested
66 if ( dataType[ 0 ] === "+" ) {
67 dataType = dataType.slice( 1 ) || "*";
68 (structure[ dataType ] = structure[ dataType ] || []).unshift( func );
72 (structure[ dataType ] = structure[ dataType ] || []).push( func );
79 // Base inspection function for prefilters and transports
80 function inspectPrefiltersOrTransports( structure, options, originalOptions, jqXHR ) {
83 seekingTransport = ( structure === transports );
85 function inspect( dataType ) {
87 inspected[ dataType ] = true;
88 jQuery.each( structure[ dataType ] || [], function( _, prefilterOrFactory ) {
89 var dataTypeOrTransport = prefilterOrFactory( options, originalOptions, jqXHR );
90 if ( typeof dataTypeOrTransport === "string" &&
91 !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 ) {
130 var firstDataType, ct, finalDataType, type,
131 contents = s.contents,
132 dataTypes = s.dataTypes;
134 // Remove auto dataType and get content-type in the process
135 while ( dataTypes[ 0 ] === "*" ) {
137 if ( ct === undefined ) {
138 ct = s.mimeType || jqXHR.getResponseHeader("Content-Type");
142 // Check if we're dealing with a known content-type
144 for ( type in contents ) {
145 if ( contents[ type ] && contents[ type ].test( ct ) ) {
146 dataTypes.unshift( type );
152 // Check to see if we have a response for the expected dataType
153 if ( dataTypes[ 0 ] in responses ) {
154 finalDataType = dataTypes[ 0 ];
156 // Try convertible dataTypes
157 for ( type in responses ) {
158 if ( !dataTypes[ 0 ] || s.converters[ type + " " + dataTypes[0] ] ) {
159 finalDataType = type;
162 if ( !firstDataType ) {
163 firstDataType = type;
166 // Or just use first one
167 finalDataType = finalDataType || firstDataType;
170 // If we found a dataType
171 // We add the dataType to the list if needed
172 // and return the corresponding response
173 if ( finalDataType ) {
174 if ( finalDataType !== dataTypes[ 0 ] ) {
175 dataTypes.unshift( finalDataType );
177 return responses[ finalDataType ];
181 /* Chain conversions given the request and the original response
182 * Also sets the responseXXX fields on the jqXHR instance
184 function ajaxConvert( s, response, jqXHR, isSuccess ) {
185 var conv2, current, conv, tmp, prev,
187 // Work with a copy of dataTypes in case we need to modify it for conversion
188 dataTypes = s.dataTypes.slice();
190 // Create converters map with lowercased keys
191 if ( dataTypes[ 1 ] ) {
192 for ( conv in s.converters ) {
193 converters[ conv.toLowerCase() ] = s.converters[ conv ];
197 current = dataTypes.shift();
199 // Convert to each sequential dataType
202 if ( s.responseFields[ current ] ) {
203 jqXHR[ s.responseFields[ current ] ] = response;
206 // Apply the dataFilter if provided
207 if ( !prev && isSuccess && s.dataFilter ) {
208 response = s.dataFilter( response, s.dataType );
212 current = dataTypes.shift();
216 // There's only work to do if current dataType is non-auto
217 if ( current === "*" ) {
221 // Convert response if prev dataType is non-auto and differs from current
222 } else if ( prev !== "*" && prev !== current ) {
224 // Seek a direct converter
225 conv = converters[ prev + " " + current ] || converters[ "* " + current ];
227 // If none found, seek a pair
229 for ( conv2 in converters ) {
231 // If conv2 outputs current
232 tmp = conv2.split( " " );
233 if ( tmp[ 1 ] === current ) {
235 // If prev can be converted to accepted input
236 conv = converters[ prev + " " + tmp[ 0 ] ] ||
237 converters[ "* " + tmp[ 0 ] ];
239 // Condense equivalence converters
240 if ( conv === true ) {
241 conv = converters[ conv2 ];
243 // Otherwise, insert the intermediate dataType
244 } else if ( converters[ conv2 ] !== true ) {
246 dataTypes.unshift( tmp[ 1 ] );
254 // Apply converter (if not an equivalence)
255 if ( conv !== true ) {
257 // Unless errors are allowed to bubble, catch and return them
258 if ( conv && s[ "throws" ] ) {
259 response = conv( response );
262 response = conv( response );
265 state: "parsererror",
266 error: conv ? e : "No conversion from " + prev + " to " + current
275 return { state: "success", data: response };
280 // Counter for holding the number of active queries
283 // Last-Modified header cache for next request
290 isLocal: rlocalProtocol.test( location.protocol ),
294 contentType: "application/x-www-form-urlencoded; charset=UTF-8",
311 xml: "application/xml, text/xml",
312 json: "application/json, text/javascript"
323 text: "responseText",
328 // Keys separate source (or catchall "*") and destination types with a single space
331 // Convert anything to text
334 // Text to html (true = no transformation)
337 // Evaluate text as a json expression
338 "text json": jQuery.parseJSON,
341 "text xml": jQuery.parseXML
344 // For options that shouldn't be deep extended:
345 // you can add your own custom options here if
346 // and when you create one that shouldn't be
347 // deep extended (see ajaxExtend)
354 // Creates a full fledged settings object into target
355 // with both ajaxSettings and settings fields.
356 // If target is omitted, writes into ajaxSettings.
357 ajaxSetup: function( target, settings ) {
360 // Building a settings object
361 ajaxExtend( ajaxExtend( target, jQuery.ajaxSettings ), settings ) :
363 // Extending ajaxSettings
364 ajaxExtend( jQuery.ajaxSettings, target );
367 ajaxPrefilter: addToPrefiltersOrTransports( prefilters ),
368 ajaxTransport: addToPrefiltersOrTransports( transports ),
371 ajax: function( url, options ) {
373 // If url is an object, simulate pre-1.5 signature
374 if ( typeof url === "object" ) {
379 // Force options to be an object
380 options = options || {};
385 // URL without anti-cache param
387 // Response headers as string
388 responseHeadersString,
393 // To know if global events are to be dispatched
399 // Create the final options object
400 s = jQuery.ajaxSetup( {}, options ),
402 callbackContext = s.context || s,
403 // Context for global events is callbackContext if it is a DOM node or jQuery collection
404 globalEventContext = s.context &&
405 ( callbackContext.nodeType || callbackContext.jquery ) ?
406 jQuery( callbackContext ) :
409 deferred = jQuery.Deferred(),
410 completeDeferred = jQuery.Callbacks("once memory"),
411 // Status-dependent callbacks
412 statusCode = s.statusCode || {},
413 // Headers (they are sent all at once)
415 requestHeadersNames = {},
418 // Default abort message
419 strAbort = "canceled",
424 // Builds headers hashtable if needed
425 getResponseHeader: function( key ) {
428 if ( !responseHeaders ) {
429 responseHeaders = {};
430 while ( (match = rheaders.exec( responseHeadersString )) ) {
431 responseHeaders[ match[1].toLowerCase() ] = match[ 2 ];
434 match = responseHeaders[ key.toLowerCase() ];
436 return match == null ? null : match;
440 getAllResponseHeaders: function() {
441 return state === 2 ? responseHeadersString : null;
445 setRequestHeader: function( name, value ) {
446 var lname = name.toLowerCase();
448 name = requestHeadersNames[ lname ] = requestHeadersNames[ lname ] || name;
449 requestHeaders[ name ] = value;
454 // Overrides response content-type header
455 overrideMimeType: function( type ) {
462 // Status-dependent callbacks
463 statusCode: function( map ) {
467 for ( code in map ) {
468 // Lazy-add the new callback in a way that preserves old ones
469 statusCode[ code ] = [ statusCode[ code ], map[ code ] ];
472 // Execute the appropriate callbacks
473 jqXHR.always( map[ jqXHR.status ] );
479 // Cancel the request
480 abort: function( statusText ) {
481 var finalText = statusText || strAbort;
483 transport.abort( finalText );
485 done( 0, finalText );
491 deferred.promise( jqXHR );
493 // Remove hash character (#7531: and string promotion)
494 // Add protocol if not provided (prefilters might expect it)
495 // Handle falsy url in the settings object (#10093: consistency with old signature)
496 // We also use the url parameter if available
497 s.url = ( ( url || s.url || location.href ) + "" ).replace( rhash, "" )
498 .replace( rprotocol, location.protocol + "//" );
500 // Alias method option to type as per ticket #12004
501 s.type = options.method || options.type || s.method || s.type;
503 // Extract dataTypes list
504 s.dataTypes = jQuery.trim( s.dataType || "*" ).toLowerCase().match( rnotwhite ) || [ "" ];
506 // A cross-domain request is in order when the origin doesn't match the current origin
507 if ( s.crossDomain == null ) {
508 urlAnchor = document.createElement( "a" );
511 // IE throws exception if url is malformed, e.g. http://example.com:80x/
513 urlAnchor.href = s.url;
515 // Anchor's host property isn't correctly set when s.url is relative
516 urlAnchor.href = urlAnchor.href;
517 s.crossDomain = originAnchor.protocol + "//" + originAnchor.host !==
518 urlAnchor.protocol + "//" + urlAnchor.host;
520 // If there is an error parsing the URL, assume it is crossDomain,
521 // it can be rejected by the transport if it is invalid
522 s.crossDomain = true;
526 // Convert data if not already a string
527 if ( s.data && s.processData && typeof s.data !== "string" ) {
528 s.data = jQuery.param( s.data, s.traditional );
532 inspectPrefiltersOrTransports( prefilters, s, options, jqXHR );
534 // If request was aborted inside a prefilter, stop there
539 // We can fire global events as of now if asked to
540 // Don't fire events if jQuery.event is undefined in an AMD-usage scenario (#15118)
541 fireGlobals = jQuery.event && s.global;
543 // Watch for a new set of requests
544 if ( fireGlobals && jQuery.active++ === 0 ) {
545 jQuery.event.trigger("ajaxStart");
548 // Uppercase the type
549 s.type = s.type.toUpperCase();
551 // Determine if request has content
552 s.hasContent = !rnoContent.test( s.type );
554 // Save the URL in case we're toying with the If-Modified-Since
555 // and/or If-None-Match header later on
558 // More options handling for requests with no content
559 if ( !s.hasContent ) {
561 // If data is available, append data to url
563 cacheURL = ( s.url += ( rquery.test( cacheURL ) ? "&" : "?" ) + s.data );
564 // #9682: remove data so that it's not used in an eventual retry
568 // Add anti-cache in url if needed
569 if ( s.cache === false ) {
570 s.url = rts.test( cacheURL ) ?
572 // If there is already a '_' parameter, set its value
573 cacheURL.replace( rts, "$1_=" + nonce++ ) :
575 // Otherwise add one to the end
576 cacheURL + ( rquery.test( cacheURL ) ? "&" : "?" ) + "_=" + nonce++;
580 // Set the If-Modified-Since and/or If-None-Match header, if in ifModified mode.
581 if ( s.ifModified ) {
582 if ( jQuery.lastModified[ cacheURL ] ) {
583 jqXHR.setRequestHeader( "If-Modified-Since", jQuery.lastModified[ cacheURL ] );
585 if ( jQuery.etag[ cacheURL ] ) {
586 jqXHR.setRequestHeader( "If-None-Match", jQuery.etag[ cacheURL ] );
590 // Set the correct header, if data is being sent
591 if ( s.data && s.hasContent && s.contentType !== false || options.contentType ) {
592 jqXHR.setRequestHeader( "Content-Type", s.contentType );
595 // Set the Accepts header for the server, depending on the dataType
596 jqXHR.setRequestHeader(
598 s.dataTypes[ 0 ] && s.accepts[ s.dataTypes[0] ] ?
599 s.accepts[ s.dataTypes[0] ] +
600 ( s.dataTypes[ 0 ] !== "*" ? ", " + allTypes + "; q=0.01" : "" ) :
604 // Check for headers option
605 for ( i in s.headers ) {
606 jqXHR.setRequestHeader( i, s.headers[ i ] );
609 // Allow custom headers/mimetypes and early abort
611 ( s.beforeSend.call( callbackContext, jqXHR, s ) === false || state === 2 ) ) {
613 // Abort if not done already and return
614 return jqXHR.abort();
617 // aborting is no longer a cancellation
620 // Install callbacks on deferreds
621 completeDeferred.add( s.complete );
622 jqXHR.done( s.success );
623 jqXHR.fail( s.error );
626 transport = inspectPrefiltersOrTransports( transports, s, options, jqXHR );
628 // If no transport, we auto-abort
630 done( -1, "No Transport" );
632 jqXHR.readyState = 1;
636 globalEventContext.trigger( "ajaxSend", [ jqXHR, s ] );
639 // If request was aborted inside ajaxSend, stop there
645 if ( s.async && s.timeout > 0 ) {
646 timeoutTimer = setTimeout(function() {
647 jqXHR.abort("timeout");
653 transport.send( requestHeaders, done );
655 // Propagate exception as error if not done
658 // Simply rethrow otherwise
665 // Callback for when everything is done
666 function done( status, nativeStatusText, responses, headers ) {
667 var isSuccess, success, error, response, modified,
668 statusText = nativeStatusText;
675 // State is "done" now
678 // Clear timeout if it exists
679 if ( timeoutTimer ) {
680 clearTimeout( timeoutTimer );
683 // Dereference transport for early garbage collection
684 // (no matter how long the jqXHR object will be used)
685 transport = undefined;
687 // Cache response headers
688 responseHeadersString = headers || "";
691 jqXHR.readyState = status > 0 ? 4 : 0;
693 // Determine if successful
694 isSuccess = status >= 200 && status < 300 || status === 304;
698 response = ajaxHandleResponses( s, jqXHR, responses );
701 // Convert no matter what (that way responseXXX fields are always set)
702 response = ajaxConvert( s, response, jqXHR, isSuccess );
704 // If successful, handle type chaining
707 // Set the If-Modified-Since and/or If-None-Match header, if in ifModified mode.
708 if ( s.ifModified ) {
709 modified = jqXHR.getResponseHeader("Last-Modified");
711 jQuery.lastModified[ cacheURL ] = modified;
713 modified = jqXHR.getResponseHeader("etag");
715 jQuery.etag[ cacheURL ] = modified;
720 if ( status === 204 || s.type === "HEAD" ) {
721 statusText = "nocontent";
724 } else if ( status === 304 ) {
725 statusText = "notmodified";
727 // If we have data, let's convert it
729 statusText = response.state;
730 success = response.data;
731 error = response.error;
735 // We extract error from statusText
736 // then normalize statusText and status 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 ) );