10 ], function( jQuery, rnotwhite, nonce, rquery ) {
18 rts = /([?&])_=[^&]*/,
19 rheaders = /^(.*?):[ \t]*([^\r\n]*)\r?$/mg, // IE leaves an \r character at EOL
20 // #7653, #8125, #8152: local protocol detection
21 rlocalProtocol = /^(?:about|app|app-storage|.+-extension|file|res|widget):$/,
22 rnoContent = /^(?:GET|HEAD)$/,
24 rurl = /^([\w.+-]+:)(?:\/\/(?:[^\/?#]*@|)([^\/?#:]*)(?::(\d+)|)|)/,
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 // #8138, IE may throw an exception when accessing
48 // a field from window.location if document.domain has been set
50 ajaxLocation = location.href;
52 // Use the href attribute of an A element
53 // since IE will modify it given document.location
54 ajaxLocation = document.createElement( "a" );
55 ajaxLocation.href = "";
56 ajaxLocation = ajaxLocation.href;
59 // Segment location into parts
60 ajaxLocParts = rurl.exec( ajaxLocation.toLowerCase() ) || [];
62 // Base "constructor" for jQuery.ajaxPrefilter and jQuery.ajaxTransport
63 function addToPrefiltersOrTransports( structure ) {
65 // dataTypeExpression is optional and defaults to "*"
66 return function( dataTypeExpression, func ) {
68 if ( typeof dataTypeExpression !== "string" ) {
69 func = dataTypeExpression;
70 dataTypeExpression = "*";
75 dataTypes = dataTypeExpression.toLowerCase().match( rnotwhite ) || [];
77 if ( jQuery.isFunction( func ) ) {
78 // For each dataType in the dataTypeExpression
79 while ( (dataType = dataTypes[i++]) ) {
80 // Prepend if requested
81 if ( dataType.charAt( 0 ) === "+" ) {
82 dataType = dataType.slice( 1 ) || "*";
83 (structure[ dataType ] = structure[ dataType ] || []).unshift( func );
87 (structure[ dataType ] = structure[ dataType ] || []).push( func );
94 // Base inspection function for prefilters and transports
95 function inspectPrefiltersOrTransports( structure, options, originalOptions, jqXHR ) {
98 seekingTransport = ( structure === transports );
100 function inspect( dataType ) {
102 inspected[ dataType ] = true;
103 jQuery.each( structure[ dataType ] || [], function( _, prefilterOrFactory ) {
104 var dataTypeOrTransport = prefilterOrFactory( options, originalOptions, jqXHR );
105 if ( typeof dataTypeOrTransport === "string" && !seekingTransport && !inspected[ dataTypeOrTransport ] ) {
106 options.dataTypes.unshift( dataTypeOrTransport );
107 inspect( dataTypeOrTransport );
109 } else if ( seekingTransport ) {
110 return !( selected = dataTypeOrTransport );
116 return inspect( options.dataTypes[ 0 ] ) || !inspected[ "*" ] && inspect( "*" );
119 // A special extend for ajax options
120 // that takes "flat" options (not to be deep extended)
122 function ajaxExtend( target, src ) {
124 flatOptions = jQuery.ajaxSettings.flatOptions || {};
127 if ( src[ key ] !== undefined ) {
128 ( flatOptions[ key ] ? target : ( deep || (deep = {}) ) )[ key ] = src[ key ];
132 jQuery.extend( true, target, deep );
138 /* Handles responses to an ajax request:
139 * - finds the right dataType (mediates between content-type and expected dataType)
140 * - returns the corresponding response
142 function ajaxHandleResponses( s, jqXHR, responses ) {
143 var firstDataType, ct, finalDataType, type,
144 contents = s.contents,
145 dataTypes = s.dataTypes;
147 // Remove auto dataType and get content-type in the process
148 while ( dataTypes[ 0 ] === "*" ) {
150 if ( ct === undefined ) {
151 ct = s.mimeType || jqXHR.getResponseHeader("Content-Type");
155 // Check if we're dealing with a known content-type
157 for ( type in contents ) {
158 if ( contents[ type ] && contents[ type ].test( ct ) ) {
159 dataTypes.unshift( type );
165 // Check to see if we have a response for the expected dataType
166 if ( dataTypes[ 0 ] in responses ) {
167 finalDataType = dataTypes[ 0 ];
169 // Try convertible dataTypes
170 for ( type in responses ) {
171 if ( !dataTypes[ 0 ] || s.converters[ type + " " + dataTypes[0] ] ) {
172 finalDataType = type;
175 if ( !firstDataType ) {
176 firstDataType = type;
179 // Or just use first one
180 finalDataType = finalDataType || firstDataType;
183 // If we found a dataType
184 // We add the dataType to the list if needed
185 // and return the corresponding response
186 if ( finalDataType ) {
187 if ( finalDataType !== dataTypes[ 0 ] ) {
188 dataTypes.unshift( finalDataType );
190 return responses[ finalDataType ];
194 /* Chain conversions given the request and the original response
195 * Also sets the responseXXX fields on the jqXHR instance
197 function ajaxConvert( s, response, jqXHR, isSuccess ) {
198 var conv2, current, conv, tmp, prev,
200 // Work with a copy of dataTypes in case we need to modify it for conversion
201 dataTypes = s.dataTypes.slice();
203 // Create converters map with lowercased keys
204 if ( dataTypes[ 1 ] ) {
205 for ( conv in s.converters ) {
206 converters[ conv.toLowerCase() ] = s.converters[ conv ];
210 current = dataTypes.shift();
212 // Convert to each sequential dataType
215 if ( s.responseFields[ current ] ) {
216 jqXHR[ s.responseFields[ current ] ] = response;
219 // Apply the dataFilter if provided
220 if ( !prev && isSuccess && s.dataFilter ) {
221 response = s.dataFilter( response, s.dataType );
225 current = dataTypes.shift();
229 // There's only work to do if current dataType is non-auto
230 if ( current === "*" ) {
234 // Convert response if prev dataType is non-auto and differs from current
235 } else if ( prev !== "*" && prev !== current ) {
237 // Seek a direct converter
238 conv = converters[ prev + " " + current ] || converters[ "* " + current ];
240 // If none found, seek a pair
242 for ( conv2 in converters ) {
244 // If conv2 outputs current
245 tmp = conv2.split( " " );
246 if ( tmp[ 1 ] === current ) {
248 // If prev can be converted to accepted input
249 conv = converters[ prev + " " + tmp[ 0 ] ] ||
250 converters[ "* " + tmp[ 0 ] ];
252 // Condense equivalence converters
253 if ( conv === true ) {
254 conv = converters[ conv2 ];
256 // Otherwise, insert the intermediate dataType
257 } else if ( converters[ conv2 ] !== true ) {
259 dataTypes.unshift( tmp[ 1 ] );
267 // Apply converter (if not an equivalence)
268 if ( conv !== true ) {
270 // Unless errors are allowed to bubble, catch and return them
271 if ( conv && s[ "throws" ] ) {
272 response = conv( response );
275 response = conv( response );
277 return { state: "parsererror", error: conv ? e : "No conversion from " + prev + " to " + current };
285 return { state: "success", data: response };
290 // Counter for holding the number of active queries
293 // Last-Modified header cache for next request
300 isLocal: rlocalProtocol.test( ajaxLocParts[ 1 ] ),
304 contentType: "application/x-www-form-urlencoded; charset=UTF-8",
321 xml: "application/xml, text/xml",
322 json: "application/json, text/javascript"
333 text: "responseText",
338 // Keys separate source (or catchall "*") and destination types with a single space
341 // Convert anything to text
344 // Text to html (true = no transformation)
347 // Evaluate text as a json expression
348 "text json": jQuery.parseJSON,
351 "text xml": jQuery.parseXML
354 // For options that shouldn't be deep extended:
355 // you can add your own custom options here if
356 // and when you create one that shouldn't be
357 // deep extended (see ajaxExtend)
364 // Creates a full fledged settings object into target
365 // with both ajaxSettings and settings fields.
366 // If target is omitted, writes into ajaxSettings.
367 ajaxSetup: function( target, settings ) {
370 // Building a settings object
371 ajaxExtend( ajaxExtend( target, jQuery.ajaxSettings ), settings ) :
373 // Extending ajaxSettings
374 ajaxExtend( jQuery.ajaxSettings, target );
377 ajaxPrefilter: addToPrefiltersOrTransports( prefilters ),
378 ajaxTransport: addToPrefiltersOrTransports( transports ),
381 ajax: function( url, options ) {
383 // If url is an object, simulate pre-1.5 signature
384 if ( typeof url === "object" ) {
389 // Force options to be an object
390 options = options || {};
392 var // Cross-domain detection vars
396 // URL without anti-cache param
398 // Response headers as string
399 responseHeadersString,
403 // To know if global events are to be dispatched
409 // Create the final options object
410 s = jQuery.ajaxSetup( {}, options ),
412 callbackContext = s.context || s,
413 // Context for global events is callbackContext if it is a DOM node or jQuery collection
414 globalEventContext = s.context && ( callbackContext.nodeType || callbackContext.jquery ) ?
415 jQuery( callbackContext ) :
418 deferred = jQuery.Deferred(),
419 completeDeferred = jQuery.Callbacks("once memory"),
420 // Status-dependent callbacks
421 statusCode = s.statusCode || {},
422 // Headers (they are sent all at once)
424 requestHeadersNames = {},
427 // Default abort message
428 strAbort = "canceled",
433 // Builds headers hashtable if needed
434 getResponseHeader: function( key ) {
437 if ( !responseHeaders ) {
438 responseHeaders = {};
439 while ( (match = rheaders.exec( responseHeadersString )) ) {
440 responseHeaders[ match[1].toLowerCase() ] = match[ 2 ];
443 match = responseHeaders[ key.toLowerCase() ];
445 return match == null ? null : match;
449 getAllResponseHeaders: function() {
450 return state === 2 ? responseHeadersString : null;
454 setRequestHeader: function( name, value ) {
455 var lname = name.toLowerCase();
457 name = requestHeadersNames[ lname ] = requestHeadersNames[ lname ] || name;
458 requestHeaders[ name ] = value;
463 // Overrides response content-type header
464 overrideMimeType: function( type ) {
471 // Status-dependent callbacks
472 statusCode: function( map ) {
476 for ( code in map ) {
477 // Lazy-add the new callback in a way that preserves old ones
478 statusCode[ code ] = [ statusCode[ code ], map[ code ] ];
481 // Execute the appropriate callbacks
482 jqXHR.always( map[ jqXHR.status ] );
488 // Cancel the request
489 abort: function( statusText ) {
490 var finalText = statusText || strAbort;
492 transport.abort( finalText );
494 done( 0, finalText );
500 deferred.promise( jqXHR ).complete = completeDeferred.add;
501 jqXHR.success = jqXHR.done;
502 jqXHR.error = jqXHR.fail;
504 // Remove hash character (#7531: and string promotion)
505 // Add protocol if not provided (#5866: IE7 issue with protocol-less urls)
506 // Handle falsy url in the settings object (#10093: consistency with old signature)
507 // We also use the url parameter if available
508 s.url = ( ( url || s.url || ajaxLocation ) + "" ).replace( rhash, "" ).replace( rprotocol, ajaxLocParts[ 1 ] + "//" );
510 // Alias method option to type as per ticket #12004
511 s.type = options.method || options.type || s.method || s.type;
513 // Extract dataTypes list
514 s.dataTypes = jQuery.trim( s.dataType || "*" ).toLowerCase().match( rnotwhite ) || [ "" ];
516 // A cross-domain request is in order when we have a protocol:host:port mismatch
517 if ( s.crossDomain == null ) {
518 parts = rurl.exec( s.url.toLowerCase() );
519 s.crossDomain = !!( parts &&
520 ( parts[ 1 ] !== ajaxLocParts[ 1 ] || parts[ 2 ] !== ajaxLocParts[ 2 ] ||
521 ( parts[ 3 ] || ( parts[ 1 ] === "http:" ? "80" : "443" ) ) !==
522 ( ajaxLocParts[ 3 ] || ( ajaxLocParts[ 1 ] === "http:" ? "80" : "443" ) ) )
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 fireGlobals = s.global;
542 // Watch for a new set of requests
543 if ( fireGlobals && jQuery.active++ === 0 ) {
544 jQuery.event.trigger("ajaxStart");
547 // Uppercase the type
548 s.type = s.type.toUpperCase();
550 // Determine if request has content
551 s.hasContent = !rnoContent.test( s.type );
553 // Save the URL in case we're toying with the If-Modified-Since
554 // and/or If-None-Match header later on
557 // More options handling for requests with no content
558 if ( !s.hasContent ) {
560 // If data is available, append data to url
562 cacheURL = ( s.url += ( rquery.test( cacheURL ) ? "&" : "?" ) + s.data );
563 // #9682: remove data so that it's not used in an eventual retry
567 // Add anti-cache in url if needed
568 if ( s.cache === false ) {
569 s.url = rts.test( cacheURL ) ?
571 // If there is already a '_' parameter, set its value
572 cacheURL.replace( rts, "$1_=" + nonce++ ) :
574 // Otherwise add one to the end
575 cacheURL + ( rquery.test( cacheURL ) ? "&" : "?" ) + "_=" + nonce++;
579 // Set the If-Modified-Since and/or If-None-Match header, if in ifModified mode.
580 if ( s.ifModified ) {
581 if ( jQuery.lastModified[ cacheURL ] ) {
582 jqXHR.setRequestHeader( "If-Modified-Since", jQuery.lastModified[ cacheURL ] );
584 if ( jQuery.etag[ cacheURL ] ) {
585 jqXHR.setRequestHeader( "If-None-Match", jQuery.etag[ cacheURL ] );
589 // Set the correct header, if data is being sent
590 if ( s.data && s.hasContent && s.contentType !== false || options.contentType ) {
591 jqXHR.setRequestHeader( "Content-Type", s.contentType );
594 // Set the Accepts header for the server, depending on the dataType
595 jqXHR.setRequestHeader(
597 s.dataTypes[ 0 ] && s.accepts[ s.dataTypes[0] ] ?
598 s.accepts[ s.dataTypes[0] ] + ( s.dataTypes[ 0 ] !== "*" ? ", " + allTypes + "; q=0.01" : "" ) :
602 // Check for headers option
603 for ( i in s.headers ) {
604 jqXHR.setRequestHeader( i, s.headers[ i ] );
607 // Allow custom headers/mimetypes and early abort
608 if ( s.beforeSend && ( s.beforeSend.call( callbackContext, jqXHR, s ) === false || state === 2 ) ) {
609 // Abort if not done already and return
610 return jqXHR.abort();
613 // aborting is no longer a cancellation
616 // Install callbacks on deferreds
617 for ( i in { success: 1, error: 1, complete: 1 } ) {
618 jqXHR[ i ]( s[ i ] );
622 transport = inspectPrefiltersOrTransports( transports, s, options, jqXHR );
624 // If no transport, we auto-abort
626 done( -1, "No Transport" );
628 jqXHR.readyState = 1;
632 globalEventContext.trigger( "ajaxSend", [ jqXHR, s ] );
635 if ( s.async && s.timeout > 0 ) {
636 timeoutTimer = setTimeout(function() {
637 jqXHR.abort("timeout");
643 transport.send( requestHeaders, done );
645 // Propagate exception as error if not done
648 // Simply rethrow otherwise
655 // Callback for when everything is done
656 function done( status, nativeStatusText, responses, headers ) {
657 var isSuccess, success, error, response, modified,
658 statusText = nativeStatusText;
665 // State is "done" now
668 // Clear timeout if it exists
669 if ( timeoutTimer ) {
670 clearTimeout( timeoutTimer );
673 // Dereference transport for early garbage collection
674 // (no matter how long the jqXHR object will be used)
675 transport = undefined;
677 // Cache response headers
678 responseHeadersString = headers || "";
681 jqXHR.readyState = status > 0 ? 4 : 0;
683 // Determine if successful
684 isSuccess = status >= 200 && status < 300 || status === 304;
688 response = ajaxHandleResponses( s, jqXHR, responses );
691 // Convert no matter what (that way responseXXX fields are always set)
692 response = ajaxConvert( s, response, jqXHR, isSuccess );
694 // If successful, handle type chaining
697 // Set the If-Modified-Since and/or If-None-Match header, if in ifModified mode.
698 if ( s.ifModified ) {
699 modified = jqXHR.getResponseHeader("Last-Modified");
701 jQuery.lastModified[ cacheURL ] = modified;
703 modified = jqXHR.getResponseHeader("etag");
705 jQuery.etag[ cacheURL ] = modified;
710 if ( status === 204 || s.type === "HEAD" ) {
711 statusText = "nocontent";
714 } else if ( status === 304 ) {
715 statusText = "notmodified";
717 // If we have data, let's convert it
719 statusText = response.state;
720 success = response.data;
721 error = response.error;
725 // We extract error from statusText
726 // then normalize statusText and status for non-aborts
728 if ( status || !statusText ) {
729 statusText = "error";
736 // Set data for the fake xhr object
737 jqXHR.status = status;
738 jqXHR.statusText = ( nativeStatusText || statusText ) + "";
742 deferred.resolveWith( callbackContext, [ success, statusText, jqXHR ] );
744 deferred.rejectWith( callbackContext, [ jqXHR, statusText, error ] );
747 // Status-dependent callbacks
748 jqXHR.statusCode( statusCode );
749 statusCode = undefined;
752 globalEventContext.trigger( isSuccess ? "ajaxSuccess" : "ajaxError",
753 [ jqXHR, s, isSuccess ? success : error ] );
757 completeDeferred.fireWith( callbackContext, [ jqXHR, statusText ] );
760 globalEventContext.trigger( "ajaxComplete", [ jqXHR, s ] );
761 // Handle the global AJAX counter
762 if ( !( --jQuery.active ) ) {
763 jQuery.event.trigger("ajaxStop");
771 getJSON: function( url, data, callback ) {
772 return jQuery.get( url, data, callback, "json" );
775 getScript: function( url, callback ) {
776 return jQuery.get( url, undefined, callback, "script" );
780 jQuery.each( [ "get", "post" ], function( i, method ) {
781 jQuery[ method ] = function( url, data, callback, type ) {
782 // shift arguments if data argument was omitted
783 if ( jQuery.isFunction( data ) ) {
784 type = type || callback;
799 // Attach a bunch of functions for handling common AJAX events
800 jQuery.each( [ "ajaxStart", "ajaxStop", "ajaxComplete", "ajaxError", "ajaxSuccess", "ajaxSend" ], function( i, type ) {
801 jQuery.fn[ type ] = function( fn ) {
802 return this.on( type, fn );