Ajax: Support binary data (including FormData)
[jquery.git] / src / ajax.js
blobdb4e301959f3132d9e8152308f6a34c7d6a9c6ad
1 import jQuery from "./core.js";
2 import document from "./var/document.js";
3 import rnothtmlwhite from "./var/rnothtmlwhite.js";
4 import location from "./ajax/var/location.js";
5 import nonce from "./ajax/var/nonce.js";
6 import rquery from "./ajax/var/rquery.js";
8 import "./core/init.js";
9 import "./core/parseXML.js";
10 import "./event/trigger.js";
11 import "./deferred.js";
12 import "./serialize.js"; // jQuery.param
14 var
15         r20 = /%20/g,
16         rhash = /#.*$/,
17         rantiCache = /([?&])_=[^&]*/,
18         rheaders = /^(.*?):[ \t]*([^\r\n]*)$/mg,
20         // trac-7653, trac-8125, trac-8152: local protocol detection
21         rlocalProtocol = /^(?:about|app|app-storage|.+-extension|file|res|widget):$/,
22         rnoContent = /^(?:GET|HEAD)$/,
23         rprotocol = /^\/\//,
25         /* Prefilters
26          * 1) They are useful to introduce custom dataTypes (see ajax/jsonp.js for an example)
27          * 2) These are called:
28          *    - BEFORE asking for a transport
29          *    - AFTER param serialization (s.data is a string if s.processData is true)
30          * 3) key is the dataType
31          * 4) the catchall symbol "*" can be used
32          * 5) execution will start with transport dataType and THEN continue down to "*" if needed
33          */
34         prefilters = {},
36         /* Transports bindings
37          * 1) key is the dataType
38          * 2) the catchall symbol "*" can be used
39          * 3) selection will start with transport dataType and THEN go to "*" if needed
40          */
41         transports = {},
43         // Avoid comment-prolog char sequence (trac-10098); must appease lint and evade compression
44         allTypes = "*/".concat( "*" ),
46         // Anchor tag for parsing the document origin
47         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 = "*";
60                 }
62                 var dataType,
63                         i = 0,
64                         dataTypes = dataTypeExpression.toLowerCase().match( rnothtmlwhite ) || [];
66                 if ( typeof func === "function" ) {
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 );
76                                 // Otherwise append
77                                 } else {
78                                         ( structure[ dataType ] = structure[ dataType ] || [] ).push( func );
79                                 }
80                         }
81                 }
82         };
85 // Base inspection function for prefilters and transports
86 function inspectPrefiltersOrTransports( structure, options, originalOptions, jqXHR ) {
88         var inspected = {},
89                 seekingTransport = ( structure === transports );
91         function inspect( dataType ) {
92                 var selected;
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 );
101                                 return false;
102                         } else if ( seekingTransport ) {
103                                 return !( selected = dataTypeOrTransport );
104                         }
105                 } );
106                 return selected;
107         }
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)
114 // Fixes trac-9887
115 function ajaxExtend( target, src ) {
116         var key, deep,
117                 flatOptions = jQuery.ajaxSettings.flatOptions || {};
119         for ( key in src ) {
120                 if ( src[ key ] !== undefined ) {
121                         ( flatOptions[ key ] ? target : ( deep || ( deep = {} ) ) )[ key ] = src[ key ];
122                 }
123         }
124         if ( deep ) {
125                 jQuery.extend( true, target, deep );
126         }
128         return target;
131 /* Handles responses to an ajax request:
132  * - finds the right dataType (mediates between content-type and expected dataType)
133  * - returns the corresponding response
134  */
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 ] === "*" ) {
143                 dataTypes.shift();
144                 if ( ct === undefined ) {
145                         ct = s.mimeType || jqXHR.getResponseHeader( "Content-Type" );
146                 }
147         }
149         // Check if we're dealing with a known content-type
150         if ( ct ) {
151                 for ( type in contents ) {
152                         if ( contents[ type ] && contents[ type ].test( ct ) ) {
153                                 dataTypes.unshift( type );
154                                 break;
155                         }
156                 }
157         }
159         // Check to see if we have a response for the expected dataType
160         if ( dataTypes[ 0 ] in responses ) {
161                 finalDataType = dataTypes[ 0 ];
162         } else {
164                 // Try convertible dataTypes
165                 for ( type in responses ) {
166                         if ( !dataTypes[ 0 ] || s.converters[ type + " " + dataTypes[ 0 ] ] ) {
167                                 finalDataType = type;
168                                 break;
169                         }
170                         if ( !firstDataType ) {
171                                 firstDataType = type;
172                         }
173                 }
175                 // Or just use first one
176                 finalDataType = finalDataType || firstDataType;
177         }
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 );
185                 }
186                 return responses[ finalDataType ];
187         }
190 /* Chain conversions given the request and the original response
191  * Also sets the responseXXX fields on the jqXHR instance
192  */
193 function ajaxConvert( s, response, jqXHR, isSuccess ) {
194         var conv2, current, conv, tmp, prev,
195                 converters = {},
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 ];
204                 }
205         }
207         current = dataTypes.shift();
209         // Convert to each sequential dataType
210         while ( current ) {
212                 if ( s.responseFields[ current ] ) {
213                         jqXHR[ s.responseFields[ current ] ] = response;
214                 }
216                 // Apply the dataFilter if provided
217                 if ( !prev && isSuccess && s.dataFilter ) {
218                         response = s.dataFilter( response, s.dataType );
219                 }
221                 prev = current;
222                 current = dataTypes.shift();
224                 if ( current ) {
226                         // There's only work to do if current dataType is non-auto
227                         if ( current === "*" ) {
229                                 current = prev;
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
238                                 if ( !conv ) {
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 ] ];
248                                                         if ( conv ) {
250                                                                 // Condense equivalence converters
251                                                                 if ( conv === true ) {
252                                                                         conv = converters[ conv2 ];
254                                                                 // Otherwise, insert the intermediate dataType
255                                                                 } else if ( converters[ conv2 ] !== true ) {
256                                                                         current = tmp[ 0 ];
257                                                                         dataTypes.unshift( tmp[ 1 ] );
258                                                                 }
259                                                                 break;
260                                                         }
261                                                 }
262                                         }
263                                 }
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 );
271                                         } else {
272                                                 try {
273                                                         response = conv( response );
274                                                 } catch ( e ) {
275                                                         return {
276                                                                 state: "parsererror",
277                                                                 error: conv ? e : "No conversion from " + prev + " to " + current
278                                                         };
279                                                 }
280                                         }
281                                 }
282                         }
283                 }
284         }
286         return { state: "success", data: response };
289 jQuery.extend( {
291         // Counter for holding the number of active queries
292         active: 0,
294         // Last-Modified header cache for next request
295         lastModified: {},
296         etag: {},
298         ajaxSettings: {
299                 url: location.href,
300                 type: "GET",
301                 isLocal: rlocalProtocol.test( location.protocol ),
302                 global: true,
303                 processData: true,
304                 async: true,
305                 contentType: "application/x-www-form-urlencoded; charset=UTF-8",
307                 /*
308                 timeout: 0,
309                 data: null,
310                 dataType: null,
311                 username: null,
312                 password: null,
313                 cache: null,
314                 throws: false,
315                 traditional: false,
316                 headers: {},
317                 */
319                 accepts: {
320                         "*": allTypes,
321                         text: "text/plain",
322                         html: "text/html",
323                         xml: "application/xml, text/xml",
324                         json: "application/json, text/javascript"
325                 },
327                 contents: {
328                         xml: /\bxml\b/,
329                         html: /\bhtml/,
330                         json: /\bjson\b/
331                 },
333                 responseFields: {
334                         xml: "responseXML",
335                         text: "responseText",
336                         json: "responseJSON"
337                 },
339                 // Data converters
340                 // Keys separate source (or catchall "*") and destination types with a single space
341                 converters: {
343                         // Convert anything to text
344                         "* text": String,
346                         // Text to html (true = no transformation)
347                         "text html": true,
349                         // Evaluate text as a json expression
350                         "text json": JSON.parse,
352                         // Parse text as xml
353                         "text xml": jQuery.parseXML
354                 },
356                 // For options that shouldn't be deep extended:
357                 // you can add your own custom options here if
358                 // and when you create one that shouldn't be
359                 // deep extended (see ajaxExtend)
360                 flatOptions: {
361                         url: true,
362                         context: true
363                 }
364         },
366         // Creates a full fledged settings object into target
367         // with both ajaxSettings and settings fields.
368         // If target is omitted, writes into ajaxSettings.
369         ajaxSetup: function( target, settings ) {
370                 return settings ?
372                         // Building a settings object
373                         ajaxExtend( ajaxExtend( target, jQuery.ajaxSettings ), settings ) :
375                         // Extending ajaxSettings
376                         ajaxExtend( jQuery.ajaxSettings, target );
377         },
379         ajaxPrefilter: addToPrefiltersOrTransports( prefilters ),
380         ajaxTransport: addToPrefiltersOrTransports( transports ),
382         // Main method
383         ajax: function( url, options ) {
385                 // If url is an object, simulate pre-1.5 signature
386                 if ( typeof url === "object" ) {
387                         options = url;
388                         url = undefined;
389                 }
391                 // Force options to be an object
392                 options = options || {};
394                 var transport,
396                         // URL without anti-cache param
397                         cacheURL,
399                         // Response headers
400                         responseHeadersString,
401                         responseHeaders,
403                         // timeout handle
404                         timeoutTimer,
406                         // Url cleanup var
407                         urlAnchor,
409                         // Request state (becomes false upon send and true upon completion)
410                         completed,
412                         // To know if global events are to be dispatched
413                         fireGlobals,
415                         // Loop variable
416                         i,
418                         // uncached part of the url
419                         uncached,
421                         // Create the final options object
422                         s = jQuery.ajaxSetup( {}, options ),
424                         // Callbacks context
425                         callbackContext = s.context || s,
427                         // Context for global events is callbackContext if it is a DOM node or jQuery collection
428                         globalEventContext = s.context &&
429                                 ( callbackContext.nodeType || callbackContext.jquery ) ?
430                                 jQuery( callbackContext ) :
431                                 jQuery.event,
433                         // Deferreds
434                         deferred = jQuery.Deferred(),
435                         completeDeferred = jQuery.Callbacks( "once memory" ),
437                         // Status-dependent callbacks
438                         statusCode = s.statusCode || {},
440                         // Headers (they are sent all at once)
441                         requestHeaders = {},
442                         requestHeadersNames = {},
444                         // Default abort message
445                         strAbort = "canceled",
447                         // Fake xhr
448                         jqXHR = {
449                                 readyState: 0,
451                                 // Builds headers hashtable if needed
452                                 getResponseHeader: function( key ) {
453                                         var match;
454                                         if ( completed ) {
455                                                 if ( !responseHeaders ) {
456                                                         responseHeaders = {};
457                                                         while ( ( match = rheaders.exec( responseHeadersString ) ) ) {
459                                                                 // Support: IE 11+
460                                                                 // `getResponseHeader( key )` in IE doesn't combine all header
461                                                                 // values for the provided key into a single result with values
462                                                                 // joined by commas as other browsers do. Instead, it returns
463                                                                 // them on separate lines.
464                                                                 responseHeaders[ match[ 1 ].toLowerCase() + " " ] =
465                                                                         ( responseHeaders[ match[ 1 ].toLowerCase() + " " ] || [] )
466                                                                                 .concat( match[ 2 ] );
467                                                         }
468                                                 }
469                                                 match = responseHeaders[ key.toLowerCase() + " " ];
470                                         }
471                                         return match == null ? null : match.join( ", " );
472                                 },
474                                 // Raw string
475                                 getAllResponseHeaders: function() {
476                                         return completed ? responseHeadersString : null;
477                                 },
479                                 // Caches the header
480                                 setRequestHeader: function( name, value ) {
481                                         if ( completed == null ) {
482                                                 name = requestHeadersNames[ name.toLowerCase() ] =
483                                                         requestHeadersNames[ name.toLowerCase() ] || name;
484                                                 requestHeaders[ name ] = value;
485                                         }
486                                         return this;
487                                 },
489                                 // Overrides response content-type header
490                                 overrideMimeType: function( type ) {
491                                         if ( completed == null ) {
492                                                 s.mimeType = type;
493                                         }
494                                         return this;
495                                 },
497                                 // Status-dependent callbacks
498                                 statusCode: function( map ) {
499                                         var code;
500                                         if ( map ) {
501                                                 if ( completed ) {
503                                                         // Execute the appropriate callbacks
504                                                         jqXHR.always( map[ jqXHR.status ] );
505                                                 } else {
507                                                         // Lazy-add the new callbacks in a way that preserves old ones
508                                                         for ( code in map ) {
509                                                                 statusCode[ code ] = [ statusCode[ code ], map[ code ] ];
510                                                         }
511                                                 }
512                                         }
513                                         return this;
514                                 },
516                                 // Cancel the request
517                                 abort: function( statusText ) {
518                                         var finalText = statusText || strAbort;
519                                         if ( transport ) {
520                                                 transport.abort( finalText );
521                                         }
522                                         done( 0, finalText );
523                                         return this;
524                                 }
525                         };
527                 // Attach deferreds
528                 deferred.promise( jqXHR );
530                 // Add protocol if not provided (prefilters might expect it)
531                 // Handle falsy url in the settings object (trac-10093: consistency with old signature)
532                 // We also use the url parameter if available
533                 s.url = ( ( url || s.url || location.href ) + "" )
534                         .replace( rprotocol, location.protocol + "//" );
536                 // Alias method option to type as per ticket trac-12004
537                 s.type = options.method || options.type || s.method || s.type;
539                 // Extract dataTypes list
540                 s.dataTypes = ( s.dataType || "*" ).toLowerCase().match( rnothtmlwhite ) || [ "" ];
542                 // A cross-domain request is in order when the origin doesn't match the current origin.
543                 if ( s.crossDomain == null ) {
544                         urlAnchor = document.createElement( "a" );
546                         // Support: IE <=8 - 11+
547                         // IE throws exception on accessing the href property if url is malformed,
548                         // e.g. http://example.com:80x/
549                         try {
550                                 urlAnchor.href = s.url;
552                                 // Support: IE <=8 - 11+
553                                 // Anchor's host property isn't correctly set when s.url is relative
554                                 urlAnchor.href = urlAnchor.href;
555                                 s.crossDomain = originAnchor.protocol + "//" + originAnchor.host !==
556                                         urlAnchor.protocol + "//" + urlAnchor.host;
557                         } catch ( e ) {
559                                 // If there is an error parsing the URL, assume it is crossDomain,
560                                 // it can be rejected by the transport if it is invalid
561                                 s.crossDomain = true;
562                         }
563                 }
565                 // Apply prefilters
566                 inspectPrefiltersOrTransports( prefilters, s, options, jqXHR );
568                 // Convert data if not already a string
569                 if ( s.data && s.processData && typeof s.data !== "string" ) {
570                         s.data = jQuery.param( s.data, s.traditional );
571                 }
573                 // If request was aborted inside a prefilter, stop there
574                 if ( completed ) {
575                         return jqXHR;
576                 }
578                 // We can fire global events as of now if asked to
579                 // Don't fire events if jQuery.event is undefined in an ESM-usage scenario (trac-15118)
580                 fireGlobals = jQuery.event && s.global;
582                 // Watch for a new set of requests
583                 if ( fireGlobals && jQuery.active++ === 0 ) {
584                         jQuery.event.trigger( "ajaxStart" );
585                 }
587                 // Uppercase the type
588                 s.type = s.type.toUpperCase();
590                 // Determine if request has content
591                 s.hasContent = !rnoContent.test( s.type );
593                 // Save the URL in case we're toying with the If-Modified-Since
594                 // and/or If-None-Match header later on
595                 // Remove hash to simplify url manipulation
596                 cacheURL = s.url.replace( rhash, "" );
598                 // More options handling for requests with no content
599                 if ( !s.hasContent ) {
601                         // Remember the hash so we can put it back
602                         uncached = s.url.slice( cacheURL.length );
604                         // If data is available and should be processed, append data to url
605                         if ( s.data && ( s.processData || typeof s.data === "string" ) ) {
606                                 cacheURL += ( rquery.test( cacheURL ) ? "&" : "?" ) + s.data;
608                                 // trac-9682: remove data so that it's not used in an eventual retry
609                                 delete s.data;
610                         }
612                         // Add or update anti-cache param if needed
613                         if ( s.cache === false ) {
614                                 cacheURL = cacheURL.replace( rantiCache, "$1" );
615                                 uncached = ( rquery.test( cacheURL ) ? "&" : "?" ) + "_=" +
616                                         ( nonce.guid++ ) + uncached;
617                         }
619                         // Put hash and anti-cache on the URL that will be requested (gh-1732)
620                         s.url = cacheURL + uncached;
622                 // Change '%20' to '+' if this is encoded form body content (gh-2658)
623                 } else if ( s.data && s.processData &&
624                         ( s.contentType || "" ).indexOf( "application/x-www-form-urlencoded" ) === 0 ) {
625                         s.data = s.data.replace( r20, "+" );
626                 }
628                 // Set the If-Modified-Since and/or If-None-Match header, if in ifModified mode.
629                 if ( s.ifModified ) {
630                         if ( jQuery.lastModified[ cacheURL ] ) {
631                                 jqXHR.setRequestHeader( "If-Modified-Since", jQuery.lastModified[ cacheURL ] );
632                         }
633                         if ( jQuery.etag[ cacheURL ] ) {
634                                 jqXHR.setRequestHeader( "If-None-Match", jQuery.etag[ cacheURL ] );
635                         }
636                 }
638                 // Set the correct header, if data is being sent
639                 if ( s.data && s.hasContent && s.contentType !== false || options.contentType ) {
640                         jqXHR.setRequestHeader( "Content-Type", s.contentType );
641                 }
643                 // Set the Accepts header for the server, depending on the dataType
644                 jqXHR.setRequestHeader(
645                         "Accept",
646                         s.dataTypes[ 0 ] && s.accepts[ s.dataTypes[ 0 ] ] ?
647                                 s.accepts[ s.dataTypes[ 0 ] ] +
648                                         ( s.dataTypes[ 0 ] !== "*" ? ", " + allTypes + "; q=0.01" : "" ) :
649                                 s.accepts[ "*" ]
650                 );
652                 // Check for headers option
653                 for ( i in s.headers ) {
654                         jqXHR.setRequestHeader( i, s.headers[ i ] );
655                 }
657                 // Allow custom headers/mimetypes and early abort
658                 if ( s.beforeSend &&
659                         ( s.beforeSend.call( callbackContext, jqXHR, s ) === false || completed ) ) {
661                         // Abort if not done already and return
662                         return jqXHR.abort();
663                 }
665                 // Aborting is no longer a cancellation
666                 strAbort = "abort";
668                 // Install callbacks on deferreds
669                 completeDeferred.add( s.complete );
670                 jqXHR.done( s.success );
671                 jqXHR.fail( s.error );
673                 // Get transport
674                 transport = inspectPrefiltersOrTransports( transports, s, options, jqXHR );
676                 // If no transport, we auto-abort
677                 if ( !transport ) {
678                         done( -1, "No Transport" );
679                 } else {
680                         jqXHR.readyState = 1;
682                         // Send global event
683                         if ( fireGlobals ) {
684                                 globalEventContext.trigger( "ajaxSend", [ jqXHR, s ] );
685                         }
687                         // If request was aborted inside ajaxSend, stop there
688                         if ( completed ) {
689                                 return jqXHR;
690                         }
692                         // Timeout
693                         if ( s.async && s.timeout > 0 ) {
694                                 timeoutTimer = window.setTimeout( function() {
695                                         jqXHR.abort( "timeout" );
696                                 }, s.timeout );
697                         }
699                         try {
700                                 completed = false;
701                                 transport.send( requestHeaders, done );
702                         } catch ( e ) {
704                                 // Rethrow post-completion exceptions
705                                 if ( completed ) {
706                                         throw e;
707                                 }
709                                 // Propagate others as results
710                                 done( -1, e );
711                         }
712                 }
714                 // Callback for when everything is done
715                 function done( status, nativeStatusText, responses, headers ) {
716                         var isSuccess, success, error, response, modified,
717                                 statusText = nativeStatusText;
719                         // Ignore repeat invocations
720                         if ( completed ) {
721                                 return;
722                         }
724                         completed = true;
726                         // Clear timeout if it exists
727                         if ( timeoutTimer ) {
728                                 window.clearTimeout( timeoutTimer );
729                         }
731                         // Dereference transport for early garbage collection
732                         // (no matter how long the jqXHR object will be used)
733                         transport = undefined;
735                         // Cache response headers
736                         responseHeadersString = headers || "";
738                         // Set readyState
739                         jqXHR.readyState = status > 0 ? 4 : 0;
741                         // Determine if successful
742                         isSuccess = status >= 200 && status < 300 || status === 304;
744                         // Get response data
745                         if ( responses ) {
746                                 response = ajaxHandleResponses( s, jqXHR, responses );
747                         }
749                         // Use a noop converter for missing script but not if jsonp
750                         if ( !isSuccess &&
751                                 jQuery.inArray( "script", s.dataTypes ) > -1 &&
752                                 jQuery.inArray( "json", s.dataTypes ) < 0 ) {
753                                 s.converters[ "text script" ] = function() {};
754                         }
756                         // Convert no matter what (that way responseXXX fields are always set)
757                         response = ajaxConvert( s, response, jqXHR, isSuccess );
759                         // If successful, handle type chaining
760                         if ( isSuccess ) {
762                                 // Set the If-Modified-Since and/or If-None-Match header, if in ifModified mode.
763                                 if ( s.ifModified ) {
764                                         modified = jqXHR.getResponseHeader( "Last-Modified" );
765                                         if ( modified ) {
766                                                 jQuery.lastModified[ cacheURL ] = modified;
767                                         }
768                                         modified = jqXHR.getResponseHeader( "etag" );
769                                         if ( modified ) {
770                                                 jQuery.etag[ cacheURL ] = modified;
771                                         }
772                                 }
774                                 // if no content
775                                 if ( status === 204 || s.type === "HEAD" ) {
776                                         statusText = "nocontent";
778                                 // if not modified
779                                 } else if ( status === 304 ) {
780                                         statusText = "notmodified";
782                                 // If we have data, let's convert it
783                                 } else {
784                                         statusText = response.state;
785                                         success = response.data;
786                                         error = response.error;
787                                         isSuccess = !error;
788                                 }
789                         } else {
791                                 // Extract error from statusText and normalize for non-aborts
792                                 error = statusText;
793                                 if ( status || !statusText ) {
794                                         statusText = "error";
795                                         if ( status < 0 ) {
796                                                 status = 0;
797                                         }
798                                 }
799                         }
801                         // Set data for the fake xhr object
802                         jqXHR.status = status;
803                         jqXHR.statusText = ( nativeStatusText || statusText ) + "";
805                         // Success/Error
806                         if ( isSuccess ) {
807                                 deferred.resolveWith( callbackContext, [ success, statusText, jqXHR ] );
808                         } else {
809                                 deferred.rejectWith( callbackContext, [ jqXHR, statusText, error ] );
810                         }
812                         // Status-dependent callbacks
813                         jqXHR.statusCode( statusCode );
814                         statusCode = undefined;
816                         if ( fireGlobals ) {
817                                 globalEventContext.trigger( isSuccess ? "ajaxSuccess" : "ajaxError",
818                                         [ jqXHR, s, isSuccess ? success : error ] );
819                         }
821                         // Complete
822                         completeDeferred.fireWith( callbackContext, [ jqXHR, statusText ] );
824                         if ( fireGlobals ) {
825                                 globalEventContext.trigger( "ajaxComplete", [ jqXHR, s ] );
827                                 // Handle the global AJAX counter
828                                 if ( !( --jQuery.active ) ) {
829                                         jQuery.event.trigger( "ajaxStop" );
830                                 }
831                         }
832                 }
834                 return jqXHR;
835         },
837         getJSON: function( url, data, callback ) {
838                 return jQuery.get( url, data, callback, "json" );
839         },
841         getScript: function( url, callback ) {
842                 return jQuery.get( url, undefined, callback, "script" );
843         }
844 } );
846 jQuery.each( [ "get", "post" ], function( _i, method ) {
847         jQuery[ method ] = function( url, data, callback, type ) {
849                 // Shift arguments if data argument was omitted.
850                 // Handle the null callback placeholder.
851                 if ( typeof data === "function" || data === null ) {
852                         type = type || callback;
853                         callback = data;
854                         data = undefined;
855                 }
857                 // The url can be an options object (which then must have .url)
858                 return jQuery.ajax( jQuery.extend( {
859                         url: url,
860                         type: method,
861                         dataType: type,
862                         data: data,
863                         success: callback
864                 }, jQuery.isPlainObject( url ) && url ) );
865         };
866 } );
868 jQuery.ajaxPrefilter( function( s ) {
869         var i;
870         for ( i in s.headers ) {
871                 if ( i.toLowerCase() === "content-type" ) {
872                         s.contentType = s.headers[ i ] || "";
873                 }
874         }
875 } );
877 export default jQuery;