Build: Fix the import path to serialize.js from ajax.js
[jquery.git] / src / ajax.js
blob3d4ce017ba9d1e1cbccc44b8060a5c74ccec5409
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 "./ajax/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         // #7653, #8125, #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 (#10098); must appease lint and evade compression
44         allTypes = "*/".concat( "*" ),
46         // Anchor tag for parsing the document origin
47         originAnchor = document.createElement( "a" );
48         originAnchor.href = location.href;
50 // Base "constructor" for jQuery.ajaxPrefilter and jQuery.ajaxTransport
51 function addToPrefiltersOrTransports( structure ) {
53         // dataTypeExpression is optional and defaults to "*"
54         return function( dataTypeExpression, func ) {
56                 if ( typeof dataTypeExpression !== "string" ) {
57                         func = dataTypeExpression;
58                         dataTypeExpression = "*";
59                 }
61                 var dataType,
62                         i = 0,
63                         dataTypes = dataTypeExpression.toLowerCase().match( rnothtmlwhite ) || [];
65                 if ( typeof func === "function" ) {
67                         // For each dataType in the dataTypeExpression
68                         while ( ( dataType = dataTypes[ i++ ] ) ) {
70                                 // Prepend if requested
71                                 if ( dataType[ 0 ] === "+" ) {
72                                         dataType = dataType.slice( 1 ) || "*";
73                                         ( structure[ dataType ] = structure[ dataType ] || [] ).unshift( func );
75                                 // Otherwise append
76                                 } else {
77                                         ( structure[ dataType ] = structure[ dataType ] || [] ).push( func );
78                                 }
79                         }
80                 }
81         };
84 // Base inspection function for prefilters and transports
85 function inspectPrefiltersOrTransports( structure, options, originalOptions, jqXHR ) {
87         var inspected = {},
88                 seekingTransport = ( structure === transports );
90         function inspect( dataType ) {
91                 var selected;
92                 inspected[ dataType ] = true;
93                 jQuery.each( structure[ dataType ] || [], function( _, prefilterOrFactory ) {
94                         var dataTypeOrTransport = prefilterOrFactory( options, originalOptions, jqXHR );
95                         if ( typeof dataTypeOrTransport === "string" &&
96                                 !seekingTransport && !inspected[ dataTypeOrTransport ] ) {
98                                 options.dataTypes.unshift( dataTypeOrTransport );
99                                 inspect( dataTypeOrTransport );
100                                 return false;
101                         } else if ( seekingTransport ) {
102                                 return !( selected = dataTypeOrTransport );
103                         }
104                 } );
105                 return selected;
106         }
108         return inspect( options.dataTypes[ 0 ] ) || !inspected[ "*" ] && inspect( "*" );
111 // A special extend for ajax options
112 // that takes "flat" options (not to be deep extended)
113 // Fixes #9887
114 function ajaxExtend( target, src ) {
115         var key, deep,
116                 flatOptions = jQuery.ajaxSettings.flatOptions || {};
118         for ( key in src ) {
119                 if ( src[ key ] !== undefined ) {
120                         ( flatOptions[ key ] ? target : ( deep || ( deep = {} ) ) )[ key ] = src[ key ];
121                 }
122         }
123         if ( deep ) {
124                 jQuery.extend( true, target, deep );
125         }
127         return target;
130 /* Handles responses to an ajax request:
131  * - finds the right dataType (mediates between content-type and expected dataType)
132  * - returns the corresponding response
133  */
134 function ajaxHandleResponses( s, jqXHR, responses ) {
136         var ct, type, finalDataType, firstDataType,
137                 contents = s.contents,
138                 dataTypes = s.dataTypes;
140         // Remove auto dataType and get content-type in the process
141         while ( dataTypes[ 0 ] === "*" ) {
142                 dataTypes.shift();
143                 if ( ct === undefined ) {
144                         ct = s.mimeType || jqXHR.getResponseHeader( "Content-Type" );
145                 }
146         }
148         // Check if we're dealing with a known content-type
149         if ( ct ) {
150                 for ( type in contents ) {
151                         if ( contents[ type ] && contents[ type ].test( ct ) ) {
152                                 dataTypes.unshift( type );
153                                 break;
154                         }
155                 }
156         }
158         // Check to see if we have a response for the expected dataType
159         if ( dataTypes[ 0 ] in responses ) {
160                 finalDataType = dataTypes[ 0 ];
161         } else {
163                 // Try convertible dataTypes
164                 for ( type in responses ) {
165                         if ( !dataTypes[ 0 ] || s.converters[ type + " " + dataTypes[ 0 ] ] ) {
166                                 finalDataType = type;
167                                 break;
168                         }
169                         if ( !firstDataType ) {
170                                 firstDataType = type;
171                         }
172                 }
174                 // Or just use first one
175                 finalDataType = finalDataType || firstDataType;
176         }
178         // If we found a dataType
179         // We add the dataType to the list if needed
180         // and return the corresponding response
181         if ( finalDataType ) {
182                 if ( finalDataType !== dataTypes[ 0 ] ) {
183                         dataTypes.unshift( finalDataType );
184                 }
185                 return responses[ finalDataType ];
186         }
189 /* Chain conversions given the request and the original response
190  * Also sets the responseXXX fields on the jqXHR instance
191  */
192 function ajaxConvert( s, response, jqXHR, isSuccess ) {
193         var conv2, current, conv, tmp, prev,
194                 converters = {},
196                 // Work with a copy of dataTypes in case we need to modify it for conversion
197                 dataTypes = s.dataTypes.slice();
199         // Create converters map with lowercased keys
200         if ( dataTypes[ 1 ] ) {
201                 for ( conv in s.converters ) {
202                         converters[ conv.toLowerCase() ] = s.converters[ conv ];
203                 }
204         }
206         current = dataTypes.shift();
208         // Convert to each sequential dataType
209         while ( current ) {
211                 if ( s.responseFields[ current ] ) {
212                         jqXHR[ s.responseFields[ current ] ] = response;
213                 }
215                 // Apply the dataFilter if provided
216                 if ( !prev && isSuccess && s.dataFilter ) {
217                         response = s.dataFilter( response, s.dataType );
218                 }
220                 prev = current;
221                 current = dataTypes.shift();
223                 if ( current ) {
225                         // There's only work to do if current dataType is non-auto
226                         if ( current === "*" ) {
228                                 current = prev;
230                         // Convert response if prev dataType is non-auto and differs from current
231                         } else if ( prev !== "*" && prev !== current ) {
233                                 // Seek a direct converter
234                                 conv = converters[ prev + " " + current ] || converters[ "* " + current ];
236                                 // If none found, seek a pair
237                                 if ( !conv ) {
238                                         for ( conv2 in converters ) {
240                                                 // If conv2 outputs current
241                                                 tmp = conv2.split( " " );
242                                                 if ( tmp[ 1 ] === current ) {
244                                                         // If prev can be converted to accepted input
245                                                         conv = converters[ prev + " " + tmp[ 0 ] ] ||
246                                                                 converters[ "* " + tmp[ 0 ] ];
247                                                         if ( conv ) {
249                                                                 // Condense equivalence converters
250                                                                 if ( conv === true ) {
251                                                                         conv = converters[ conv2 ];
253                                                                 // Otherwise, insert the intermediate dataType
254                                                                 } else if ( converters[ conv2 ] !== true ) {
255                                                                         current = tmp[ 0 ];
256                                                                         dataTypes.unshift( tmp[ 1 ] );
257                                                                 }
258                                                                 break;
259                                                         }
260                                                 }
261                                         }
262                                 }
264                                 // Apply converter (if not an equivalence)
265                                 if ( conv !== true ) {
267                                         // Unless errors are allowed to bubble, catch and return them
268                                         if ( conv && s.throws ) {
269                                                 response = conv( response );
270                                         } else {
271                                                 try {
272                                                         response = conv( response );
273                                                 } catch ( e ) {
274                                                         return {
275                                                                 state: "parsererror",
276                                                                 error: conv ? e : "No conversion from " + prev + " to " + current
277                                                         };
278                                                 }
279                                         }
280                                 }
281                         }
282                 }
283         }
285         return { state: "success", data: response };
288 jQuery.extend( {
290         // Counter for holding the number of active queries
291         active: 0,
293         // Last-Modified header cache for next request
294         lastModified: {},
295         etag: {},
297         ajaxSettings: {
298                 url: location.href,
299                 type: "GET",
300                 isLocal: rlocalProtocol.test( location.protocol ),
301                 global: true,
302                 processData: true,
303                 async: true,
304                 contentType: "application/x-www-form-urlencoded; charset=UTF-8",
306                 /*
307                 timeout: 0,
308                 data: null,
309                 dataType: null,
310                 username: null,
311                 password: null,
312                 cache: null,
313                 throws: false,
314                 traditional: false,
315                 headers: {},
316                 */
318                 accepts: {
319                         "*": allTypes,
320                         text: "text/plain",
321                         html: "text/html",
322                         xml: "application/xml, text/xml",
323                         json: "application/json, text/javascript"
324                 },
326                 contents: {
327                         xml: /\bxml\b/,
328                         html: /\bhtml/,
329                         json: /\bjson\b/
330                 },
332                 responseFields: {
333                         xml: "responseXML",
334                         text: "responseText",
335                         json: "responseJSON"
336                 },
338                 // Data converters
339                 // Keys separate source (or catchall "*") and destination types with a single space
340                 converters: {
342                         // Convert anything to text
343                         "* text": String,
345                         // Text to html (true = no transformation)
346                         "text html": true,
348                         // Evaluate text as a json expression
349                         "text json": JSON.parse,
351                         // Parse text as xml
352                         "text xml": jQuery.parseXML
353                 },
355                 // For options that shouldn't be deep extended:
356                 // you can add your own custom options here if
357                 // and when you create one that shouldn't be
358                 // deep extended (see ajaxExtend)
359                 flatOptions: {
360                         url: true,
361                         context: true
362                 }
363         },
365         // Creates a full fledged settings object into target
366         // with both ajaxSettings and settings fields.
367         // If target is omitted, writes into ajaxSettings.
368         ajaxSetup: function( target, settings ) {
369                 return settings ?
371                         // Building a settings object
372                         ajaxExtend( ajaxExtend( target, jQuery.ajaxSettings ), settings ) :
374                         // Extending ajaxSettings
375                         ajaxExtend( jQuery.ajaxSettings, target );
376         },
378         ajaxPrefilter: addToPrefiltersOrTransports( prefilters ),
379         ajaxTransport: addToPrefiltersOrTransports( transports ),
381         // Main method
382         ajax: function( url, options ) {
384                 // If url is an object, simulate pre-1.5 signature
385                 if ( typeof url === "object" ) {
386                         options = url;
387                         url = undefined;
388                 }
390                 // Force options to be an object
391                 options = options || {};
393                 var transport,
395                         // URL without anti-cache param
396                         cacheURL,
398                         // Response headers
399                         responseHeadersString,
400                         responseHeaders,
402                         // timeout handle
403                         timeoutTimer,
405                         // Url cleanup var
406                         urlAnchor,
408                         // Request state (becomes false upon send and true upon completion)
409                         completed,
411                         // To know if global events are to be dispatched
412                         fireGlobals,
414                         // Loop variable
415                         i,
417                         // uncached part of the url
418                         uncached,
420                         // Create the final options object
421                         s = jQuery.ajaxSetup( {}, options ),
423                         // Callbacks context
424                         callbackContext = s.context || s,
426                         // Context for global events is callbackContext if it is a DOM node or jQuery collection
427                         globalEventContext = s.context &&
428                                 ( callbackContext.nodeType || callbackContext.jquery ) ?
429                                         jQuery( callbackContext ) :
430                                         jQuery.event,
432                         // Deferreds
433                         deferred = jQuery.Deferred(),
434                         completeDeferred = jQuery.Callbacks( "once memory" ),
436                         // Status-dependent callbacks
437                         statusCode = s.statusCode || {},
439                         // Headers (they are sent all at once)
440                         requestHeaders = {},
441                         requestHeadersNames = {},
443                         // Default abort message
444                         strAbort = "canceled",
446                         // Fake xhr
447                         jqXHR = {
448                                 readyState: 0,
450                                 // Builds headers hashtable if needed
451                                 getResponseHeader: function( key ) {
452                                         var match;
453                                         if ( completed ) {
454                                                 if ( !responseHeaders ) {
455                                                         responseHeaders = {};
456                                                         while ( ( match = rheaders.exec( responseHeadersString ) ) ) {
458                                                                 // Support: IE 11+
459                                                                 // `getResponseHeader( key )` in IE doesn't combine all header
460                                                                 // values for the provided key into a single result with values
461                                                                 // joined by commas as other browsers do. Instead, it returns
462                                                                 // them on separate lines.
463                                                                 responseHeaders[ match[ 1 ].toLowerCase() + " " ] =
464                                                                         ( responseHeaders[ match[ 1 ].toLowerCase() + " " ] || [] )
465                                                                                 .concat( match[ 2 ] );
466                                                         }
467                                                 }
468                                                 match = responseHeaders[ key.toLowerCase() + " " ];
469                                         }
470                                         return match == null ? null : match.join( ", " );
471                                 },
473                                 // Raw string
474                                 getAllResponseHeaders: function() {
475                                         return completed ? responseHeadersString : null;
476                                 },
478                                 // Caches the header
479                                 setRequestHeader: function( name, value ) {
480                                         if ( completed == null ) {
481                                                 name = requestHeadersNames[ name.toLowerCase() ] =
482                                                         requestHeadersNames[ name.toLowerCase() ] || name;
483                                                 requestHeaders[ name ] = value;
484                                         }
485                                         return this;
486                                 },
488                                 // Overrides response content-type header
489                                 overrideMimeType: function( type ) {
490                                         if ( completed == null ) {
491                                                 s.mimeType = type;
492                                         }
493                                         return this;
494                                 },
496                                 // Status-dependent callbacks
497                                 statusCode: function( map ) {
498                                         var code;
499                                         if ( map ) {
500                                                 if ( completed ) {
502                                                         // Execute the appropriate callbacks
503                                                         jqXHR.always( map[ jqXHR.status ] );
504                                                 } else {
506                                                         // Lazy-add the new callbacks in a way that preserves old ones
507                                                         for ( code in map ) {
508                                                                 statusCode[ code ] = [ statusCode[ code ], map[ code ] ];
509                                                         }
510                                                 }
511                                         }
512                                         return this;
513                                 },
515                                 // Cancel the request
516                                 abort: function( statusText ) {
517                                         var finalText = statusText || strAbort;
518                                         if ( transport ) {
519                                                 transport.abort( finalText );
520                                         }
521                                         done( 0, finalText );
522                                         return this;
523                                 }
524                         };
526                 // Attach deferreds
527                 deferred.promise( jqXHR );
529                 // Add protocol if not provided (prefilters might expect it)
530                 // Handle falsy url in the settings object (#10093: consistency with old signature)
531                 // We also use the url parameter if available
532                 s.url = ( ( url || s.url || location.href ) + "" )
533                         .replace( rprotocol, location.protocol + "//" );
535                 // Alias method option to type as per ticket #12004
536                 s.type = options.method || options.type || s.method || s.type;
538                 // Extract dataTypes list
539                 s.dataTypes = ( s.dataType || "*" ).toLowerCase().match( rnothtmlwhite ) || [ "" ];
541                 // A cross-domain request is in order when the origin doesn't match the current origin.
542                 if ( s.crossDomain == null ) {
543                         urlAnchor = document.createElement( "a" );
545                         // Support: IE <=8 - 11+, Edge 12 - 17 only
546                         // IE throws exception on accessing the href property if url is malformed,
547                         // e.g. http://example.com:80x/
548                         try {
549                                 urlAnchor.href = s.url;
551                                 // Support: IE <=8 - 11+
552                                 // Anchor's host property isn't correctly set when s.url is relative
553                                 urlAnchor.href = urlAnchor.href;
554                                 s.crossDomain = originAnchor.protocol + "//" + originAnchor.host !==
555                                         urlAnchor.protocol + "//" + urlAnchor.host;
556                         } catch ( e ) {
558                                 // If there is an error parsing the URL, assume it is crossDomain,
559                                 // it can be rejected by the transport if it is invalid
560                                 s.crossDomain = true;
561                         }
562                 }
564                 // Convert data if not already a string
565                 if ( s.data && s.processData && typeof s.data !== "string" ) {
566                         s.data = jQuery.param( s.data, s.traditional );
567                 }
569                 // Apply prefilters
570                 inspectPrefiltersOrTransports( prefilters, s, options, jqXHR );
572                 // If request was aborted inside a prefilter, stop there
573                 if ( completed ) {
574                         return jqXHR;
575                 }
577                 // We can fire global events as of now if asked to
578                 // Don't fire events if jQuery.event is undefined in an AMD-usage scenario (#15118)
579                 fireGlobals = jQuery.event && s.global;
581                 // Watch for a new set of requests
582                 if ( fireGlobals && jQuery.active++ === 0 ) {
583                         jQuery.event.trigger( "ajaxStart" );
584                 }
586                 // Uppercase the type
587                 s.type = s.type.toUpperCase();
589                 // Determine if request has content
590                 s.hasContent = !rnoContent.test( s.type );
592                 // Save the URL in case we're toying with the If-Modified-Since
593                 // and/or If-None-Match header later on
594                 // Remove hash to simplify url manipulation
595                 cacheURL = s.url.replace( rhash, "" );
597                 // More options handling for requests with no content
598                 if ( !s.hasContent ) {
600                         // Remember the hash so we can put it back
601                         uncached = s.url.slice( cacheURL.length );
603                         // If data is available and should be processed, append data to url
604                         if ( s.data && ( s.processData || typeof s.data === "string" ) ) {
605                                 cacheURL += ( rquery.test( cacheURL ) ? "&" : "?" ) + s.data;
607                                 // #9682: remove data so that it's not used in an eventual retry
608                                 delete s.data;
609                         }
611                         // Add or update anti-cache param if needed
612                         if ( s.cache === false ) {
613                                 cacheURL = cacheURL.replace( rantiCache, "$1" );
614                                 uncached = ( rquery.test( cacheURL ) ? "&" : "?" ) + "_=" +
615                                         ( nonce.guid++ ) + uncached;
616                         }
618                         // Put hash and anti-cache on the URL that will be requested (gh-1732)
619                         s.url = cacheURL + uncached;
621                 // Change '%20' to '+' if this is encoded form body content (gh-2658)
622                 } else if ( s.data && s.processData &&
623                         ( s.contentType || "" ).indexOf( "application/x-www-form-urlencoded" ) === 0 ) {
624                         s.data = s.data.replace( r20, "+" );
625                 }
627                 // Set the If-Modified-Since and/or If-None-Match header, if in ifModified mode.
628                 if ( s.ifModified ) {
629                         if ( jQuery.lastModified[ cacheURL ] ) {
630                                 jqXHR.setRequestHeader( "If-Modified-Since", jQuery.lastModified[ cacheURL ] );
631                         }
632                         if ( jQuery.etag[ cacheURL ] ) {
633                                 jqXHR.setRequestHeader( "If-None-Match", jQuery.etag[ cacheURL ] );
634                         }
635                 }
637                 // Set the correct header, if data is being sent
638                 if ( s.data && s.hasContent && s.contentType !== false || options.contentType ) {
639                         jqXHR.setRequestHeader( "Content-Type", s.contentType );
640                 }
642                 // Set the Accepts header for the server, depending on the dataType
643                 jqXHR.setRequestHeader(
644                         "Accept",
645                         s.dataTypes[ 0 ] && s.accepts[ s.dataTypes[ 0 ] ] ?
646                                 s.accepts[ s.dataTypes[ 0 ] ] +
647                                         ( s.dataTypes[ 0 ] !== "*" ? ", " + allTypes + "; q=0.01" : "" ) :
648                                 s.accepts[ "*" ]
649                 );
651                 // Check for headers option
652                 for ( i in s.headers ) {
653                         jqXHR.setRequestHeader( i, s.headers[ i ] );
654                 }
656                 // Allow custom headers/mimetypes and early abort
657                 if ( s.beforeSend &&
658                         ( s.beforeSend.call( callbackContext, jqXHR, s ) === false || completed ) ) {
660                         // Abort if not done already and return
661                         return jqXHR.abort();
662                 }
664                 // Aborting is no longer a cancellation
665                 strAbort = "abort";
667                 // Install callbacks on deferreds
668                 completeDeferred.add( s.complete );
669                 jqXHR.done( s.success );
670                 jqXHR.fail( s.error );
672                 // Get transport
673                 transport = inspectPrefiltersOrTransports( transports, s, options, jqXHR );
675                 // If no transport, we auto-abort
676                 if ( !transport ) {
677                         done( -1, "No Transport" );
678                 } else {
679                         jqXHR.readyState = 1;
681                         // Send global event
682                         if ( fireGlobals ) {
683                                 globalEventContext.trigger( "ajaxSend", [ jqXHR, s ] );
684                         }
686                         // If request was aborted inside ajaxSend, stop there
687                         if ( completed ) {
688                                 return jqXHR;
689                         }
691                         // Timeout
692                         if ( s.async && s.timeout > 0 ) {
693                                 timeoutTimer = window.setTimeout( function() {
694                                         jqXHR.abort( "timeout" );
695                                 }, s.timeout );
696                         }
698                         try {
699                                 completed = false;
700                                 transport.send( requestHeaders, done );
701                         } catch ( e ) {
703                                 // Rethrow post-completion exceptions
704                                 if ( completed ) {
705                                         throw e;
706                                 }
708                                 // Propagate others as results
709                                 done( -1, e );
710                         }
711                 }
713                 // Callback for when everything is done
714                 function done( status, nativeStatusText, responses, headers ) {
715                         var isSuccess, success, error, response, modified,
716                                 statusText = nativeStatusText;
718                         // Ignore repeat invocations
719                         if ( completed ) {
720                                 return;
721                         }
723                         completed = true;
725                         // Clear timeout if it exists
726                         if ( timeoutTimer ) {
727                                 window.clearTimeout( timeoutTimer );
728                         }
730                         // Dereference transport for early garbage collection
731                         // (no matter how long the jqXHR object will be used)
732                         transport = undefined;
734                         // Cache response headers
735                         responseHeadersString = headers || "";
737                         // Set readyState
738                         jqXHR.readyState = status > 0 ? 4 : 0;
740                         // Determine if successful
741                         isSuccess = status >= 200 && status < 300 || status === 304;
743                         // Get response data
744                         if ( responses ) {
745                                 response = ajaxHandleResponses( s, jqXHR, responses );
746                         }
748                         // Use a noop converter for missing script
749                         if ( !isSuccess && jQuery.inArray( "script", s.dataTypes ) > -1 ) {
750                                 s.converters[ "text script" ] = function() {};
751                         }
753                         // Convert no matter what (that way responseXXX fields are always set)
754                         response = ajaxConvert( s, response, jqXHR, isSuccess );
756                         // If successful, handle type chaining
757                         if ( isSuccess ) {
759                                 // Set the If-Modified-Since and/or If-None-Match header, if in ifModified mode.
760                                 if ( s.ifModified ) {
761                                         modified = jqXHR.getResponseHeader( "Last-Modified" );
762                                         if ( modified ) {
763                                                 jQuery.lastModified[ cacheURL ] = modified;
764                                         }
765                                         modified = jqXHR.getResponseHeader( "etag" );
766                                         if ( modified ) {
767                                                 jQuery.etag[ cacheURL ] = modified;
768                                         }
769                                 }
771                                 // if no content
772                                 if ( status === 204 || s.type === "HEAD" ) {
773                                         statusText = "nocontent";
775                                 // if not modified
776                                 } else if ( status === 304 ) {
777                                         statusText = "notmodified";
779                                 // If we have data, let's convert it
780                                 } else {
781                                         statusText = response.state;
782                                         success = response.data;
783                                         error = response.error;
784                                         isSuccess = !error;
785                                 }
786                         } else {
788                                 // Extract error from statusText and normalize for non-aborts
789                                 error = statusText;
790                                 if ( status || !statusText ) {
791                                         statusText = "error";
792                                         if ( status < 0 ) {
793                                                 status = 0;
794                                         }
795                                 }
796                         }
798                         // Set data for the fake xhr object
799                         jqXHR.status = status;
800                         jqXHR.statusText = ( nativeStatusText || statusText ) + "";
802                         // Success/Error
803                         if ( isSuccess ) {
804                                 deferred.resolveWith( callbackContext, [ success, statusText, jqXHR ] );
805                         } else {
806                                 deferred.rejectWith( callbackContext, [ jqXHR, statusText, error ] );
807                         }
809                         // Status-dependent callbacks
810                         jqXHR.statusCode( statusCode );
811                         statusCode = undefined;
813                         if ( fireGlobals ) {
814                                 globalEventContext.trigger( isSuccess ? "ajaxSuccess" : "ajaxError",
815                                         [ jqXHR, s, isSuccess ? success : error ] );
816                         }
818                         // Complete
819                         completeDeferred.fireWith( callbackContext, [ jqXHR, statusText ] );
821                         if ( fireGlobals ) {
822                                 globalEventContext.trigger( "ajaxComplete", [ jqXHR, s ] );
824                                 // Handle the global AJAX counter
825                                 if ( !( --jQuery.active ) ) {
826                                         jQuery.event.trigger( "ajaxStop" );
827                                 }
828                         }
829                 }
831                 return jqXHR;
832         },
834         getJSON: function( url, data, callback ) {
835                 return jQuery.get( url, data, callback, "json" );
836         },
838         getScript: function( url, callback ) {
839                 return jQuery.get( url, undefined, callback, "script" );
840         }
841 } );
843 jQuery.each( [ "get", "post" ], function( _i, method ) {
844         jQuery[ method ] = function( url, data, callback, type ) {
846                 // Shift arguments if data argument was omitted
847                 if ( typeof data === "function" ) {
848                         type = type || callback;
849                         callback = data;
850                         data = undefined;
851                 }
853                 // The url can be an options object (which then must have .url)
854                 return jQuery.ajax( jQuery.extend( {
855                         url: url,
856                         type: method,
857                         dataType: type,
858                         data: data,
859                         success: callback
860                 }, jQuery.isPlainObject( url ) && url ) );
861         };
862 } );
864 export default jQuery;