Selector: add test for jQuery.unique() alias
[jquery.git] / src / ajax.js
blob70bb49691a592e2f55973dbc76efd305cb0fa939
1 define([
2         "./core",
3         "./var/document",
4         "./var/rnotwhite",
5         "./ajax/var/location",
6         "./ajax/var/nonce",
7         "./ajax/var/rquery",
8         "./core/init",
9         "./ajax/parseJSON",
10         "./ajax/parseXML",
11         "./deferred"
12 ], function( jQuery, document, rnotwhite, location, nonce, rquery ) {
14 var
15         rhash = /#.*$/,
16         rts = /([?&])_=[^&]*/,
17         rheaders = /^(.*?):[ \t]*([^\r\n]*)$/mg,
18         // #7653, #8125, #8152: local protocol detection
19         rlocalProtocol = /^(?:about|app|app-storage|.+-extension|file|res|widget):$/,
20         rnoContent = /^(?:GET|HEAD)$/,
21         rprotocol = /^\/\//,
23         /* Prefilters
24          * 1) They are useful to introduce custom dataTypes (see ajax/jsonp.js for an example)
25          * 2) These are called:
26          *    - BEFORE asking for a transport
27          *    - AFTER param serialization (s.data is a string if s.processData is true)
28          * 3) key is the dataType
29          * 4) the catchall symbol "*" can be used
30          * 5) execution will start with transport dataType and THEN continue down to "*" if needed
31          */
32         prefilters = {},
34         /* Transports bindings
35          * 1) key is the dataType
36          * 2) the catchall symbol "*" can be used
37          * 3) selection will start with transport dataType and THEN go to "*" if needed
38          */
39         transports = {},
41         // Avoid comment-prolog char sequence (#10098); must appease lint and evade compression
42         allTypes = "*/".concat( "*" ),
44         // Anchor tag for parsing the document origin
45         originAnchor = document.createElement( "a" );
46         originAnchor.href = location.href;
48 // Base "constructor" for jQuery.ajaxPrefilter and jQuery.ajaxTransport
49 function addToPrefiltersOrTransports( structure ) {
51         // dataTypeExpression is optional and defaults to "*"
52         return function( dataTypeExpression, func ) {
54                 if ( typeof dataTypeExpression !== "string" ) {
55                         func = dataTypeExpression;
56                         dataTypeExpression = "*";
57                 }
59                 var dataType,
60                         i = 0,
61                         dataTypes = dataTypeExpression.toLowerCase().match( rnotwhite ) || [];
63                 if ( jQuery.isFunction( func ) ) {
64                         // For each dataType in the dataTypeExpression
65                         while ( (dataType = dataTypes[i++]) ) {
66                                 // Prepend if requested
67                                 if ( dataType[0] === "+" ) {
68                                         dataType = dataType.slice( 1 ) || "*";
69                                         (structure[ dataType ] = structure[ dataType ] || []).unshift( func );
71                                 // Otherwise append
72                                 } else {
73                                         (structure[ dataType ] = structure[ dataType ] || []).push( func );
74                                 }
75                         }
76                 }
77         };
80 // Base inspection function for prefilters and transports
81 function inspectPrefiltersOrTransports( structure, options, originalOptions, jqXHR ) {
83         var inspected = {},
84                 seekingTransport = ( structure === transports );
86         function inspect( dataType ) {
87                 var selected;
88                 inspected[ dataType ] = true;
89                 jQuery.each( structure[ dataType ] || [], function( _, prefilterOrFactory ) {
90                         var dataTypeOrTransport = prefilterOrFactory( options, originalOptions, jqXHR );
91                         if ( typeof dataTypeOrTransport === "string" &&
92                                 !seekingTransport && !inspected[ dataTypeOrTransport ] ) {
94                                 options.dataTypes.unshift( dataTypeOrTransport );
95                                 inspect( dataTypeOrTransport );
96                                 return false;
97                         } else if ( seekingTransport ) {
98                                 return !( selected = dataTypeOrTransport );
99                         }
100                 });
101                 return selected;
102         }
104         return inspect( options.dataTypes[ 0 ] ) || !inspected[ "*" ] && inspect( "*" );
107 // A special extend for ajax options
108 // that takes "flat" options (not to be deep extended)
109 // Fixes #9887
110 function ajaxExtend( target, src ) {
111         var key, deep,
112                 flatOptions = jQuery.ajaxSettings.flatOptions || {};
114         for ( key in src ) {
115                 if ( src[ key ] !== undefined ) {
116                         ( flatOptions[ key ] ? target : ( deep || (deep = {}) ) )[ key ] = src[ key ];
117                 }
118         }
119         if ( deep ) {
120                 jQuery.extend( true, target, deep );
121         }
123         return target;
126 /* Handles responses to an ajax request:
127  * - finds the right dataType (mediates between content-type and expected dataType)
128  * - returns the corresponding response
129  */
130 function ajaxHandleResponses( s, jqXHR, responses ) {
132         var ct, type, finalDataType, firstDataType,
133                 contents = s.contents,
134                 dataTypes = s.dataTypes;
136         // Remove auto dataType and get content-type in the process
137         while ( dataTypes[ 0 ] === "*" ) {
138                 dataTypes.shift();
139                 if ( ct === undefined ) {
140                         ct = s.mimeType || jqXHR.getResponseHeader("Content-Type");
141                 }
142         }
144         // Check if we're dealing with a known content-type
145         if ( ct ) {
146                 for ( type in contents ) {
147                         if ( contents[ type ] && contents[ type ].test( ct ) ) {
148                                 dataTypes.unshift( type );
149                                 break;
150                         }
151                 }
152         }
154         // Check to see if we have a response for the expected dataType
155         if ( dataTypes[ 0 ] in responses ) {
156                 finalDataType = dataTypes[ 0 ];
157         } else {
158                 // Try convertible dataTypes
159                 for ( type in responses ) {
160                         if ( !dataTypes[ 0 ] || s.converters[ type + " " + dataTypes[0] ] ) {
161                                 finalDataType = type;
162                                 break;
163                         }
164                         if ( !firstDataType ) {
165                                 firstDataType = type;
166                         }
167                 }
168                 // Or just use first one
169                 finalDataType = finalDataType || firstDataType;
170         }
172         // If we found a dataType
173         // We add the dataType to the list if needed
174         // and return the corresponding response
175         if ( finalDataType ) {
176                 if ( finalDataType !== dataTypes[ 0 ] ) {
177                         dataTypes.unshift( finalDataType );
178                 }
179                 return responses[ finalDataType ];
180         }
183 /* Chain conversions given the request and the original response
184  * Also sets the responseXXX fields on the jqXHR instance
185  */
186 function ajaxConvert( s, response, jqXHR, isSuccess ) {
187         var conv2, current, conv, tmp, prev,
188                 converters = {},
189                 // Work with a copy of dataTypes in case we need to modify it for conversion
190                 dataTypes = s.dataTypes.slice();
192         // Create converters map with lowercased keys
193         if ( dataTypes[ 1 ] ) {
194                 for ( conv in s.converters ) {
195                         converters[ conv.toLowerCase() ] = s.converters[ conv ];
196                 }
197         }
199         current = dataTypes.shift();
201         // Convert to each sequential dataType
202         while ( current ) {
204                 if ( s.responseFields[ current ] ) {
205                         jqXHR[ s.responseFields[ current ] ] = response;
206                 }
208                 // Apply the dataFilter if provided
209                 if ( !prev && isSuccess && s.dataFilter ) {
210                         response = s.dataFilter( response, s.dataType );
211                 }
213                 prev = current;
214                 current = dataTypes.shift();
216                 if ( current ) {
218                 // There's only work to do if current dataType is non-auto
219                         if ( current === "*" ) {
221                                 current = prev;
223                         // Convert response if prev dataType is non-auto and differs from current
224                         } else if ( prev !== "*" && prev !== current ) {
226                                 // Seek a direct converter
227                                 conv = converters[ prev + " " + current ] || converters[ "* " + current ];
229                                 // If none found, seek a pair
230                                 if ( !conv ) {
231                                         for ( conv2 in converters ) {
233                                                 // If conv2 outputs current
234                                                 tmp = conv2.split( " " );
235                                                 if ( tmp[ 1 ] === current ) {
237                                                         // If prev can be converted to accepted input
238                                                         conv = converters[ prev + " " + tmp[ 0 ] ] ||
239                                                                 converters[ "* " + tmp[ 0 ] ];
240                                                         if ( conv ) {
241                                                                 // Condense equivalence converters
242                                                                 if ( conv === true ) {
243                                                                         conv = converters[ conv2 ];
245                                                                 // Otherwise, insert the intermediate dataType
246                                                                 } else if ( converters[ conv2 ] !== true ) {
247                                                                         current = tmp[ 0 ];
248                                                                         dataTypes.unshift( tmp[ 1 ] );
249                                                                 }
250                                                                 break;
251                                                         }
252                                                 }
253                                         }
254                                 }
256                                 // Apply converter (if not an equivalence)
257                                 if ( conv !== true ) {
259                                         // Unless errors are allowed to bubble, catch and return them
260                                         if ( conv && s[ "throws" ] ) {
261                                                 response = conv( response );
262                                         } else {
263                                                 try {
264                                                         response = conv( response );
265                                                 } catch ( e ) {
266                                                         return {
267                                                                 state: "parsererror",
268                                                                 error: conv ? e : "No conversion from " + prev + " to " + current
269                                                         };
270                                                 }
271                                         }
272                                 }
273                         }
274                 }
275         }
277         return { state: "success", data: response };
280 jQuery.extend({
282         // Counter for holding the number of active queries
283         active: 0,
285         // Last-Modified header cache for next request
286         lastModified: {},
287         etag: {},
289         ajaxSettings: {
290                 url: location.href,
291                 type: "GET",
292                 isLocal: rlocalProtocol.test( location.protocol ),
293                 global: true,
294                 processData: true,
295                 async: true,
296                 contentType: "application/x-www-form-urlencoded; charset=UTF-8",
297                 /*
298                 timeout: 0,
299                 data: null,
300                 dataType: null,
301                 username: null,
302                 password: null,
303                 cache: null,
304                 throws: false,
305                 traditional: false,
306                 headers: {},
307                 */
309                 accepts: {
310                         "*": allTypes,
311                         text: "text/plain",
312                         html: "text/html",
313                         xml: "application/xml, text/xml",
314                         json: "application/json, text/javascript"
315                 },
317                 contents: {
318                         xml: /xml/,
319                         html: /html/,
320                         json: /json/
321                 },
323                 responseFields: {
324                         xml: "responseXML",
325                         text: "responseText",
326                         json: "responseJSON"
327                 },
329                 // Data converters
330                 // Keys separate source (or catchall "*") and destination types with a single space
331                 converters: {
333                         // Convert anything to text
334                         "* text": String,
336                         // Text to html (true = no transformation)
337                         "text html": true,
339                         // Evaluate text as a json expression
340                         "text json": jQuery.parseJSON,
342                         // Parse text as xml
343                         "text xml": jQuery.parseXML
344                 },
346                 // For options that shouldn't be deep extended:
347                 // you can add your own custom options here if
348                 // and when you create one that shouldn't be
349                 // deep extended (see ajaxExtend)
350                 flatOptions: {
351                         url: true,
352                         context: true
353                 }
354         },
356         // Creates a full fledged settings object into target
357         // with both ajaxSettings and settings fields.
358         // If target is omitted, writes into ajaxSettings.
359         ajaxSetup: function( target, settings ) {
360                 return settings ?
362                         // Building a settings object
363                         ajaxExtend( ajaxExtend( target, jQuery.ajaxSettings ), settings ) :
365                         // Extending ajaxSettings
366                         ajaxExtend( jQuery.ajaxSettings, target );
367         },
369         ajaxPrefilter: addToPrefiltersOrTransports( prefilters ),
370         ajaxTransport: addToPrefiltersOrTransports( transports ),
372         // Main method
373         ajax: function( url, options ) {
375                 // If url is an object, simulate pre-1.5 signature
376                 if ( typeof url === "object" ) {
377                         options = url;
378                         url = undefined;
379                 }
381                 // Force options to be an object
382                 options = options || {};
384                 var transport,
385                         // URL without anti-cache param
386                         cacheURL,
387                         // Response headers
388                         responseHeadersString,
389                         responseHeaders,
390                         // timeout handle
391                         timeoutTimer,
392                         // Url cleanup var
393                         urlAnchor,
394                         // To know if global events are to be dispatched
395                         fireGlobals,
396                         // Loop variable
397                         i,
398                         // Create the final options object
399                         s = jQuery.ajaxSetup( {}, options ),
400                         // Callbacks context
401                         callbackContext = s.context || s,
402                         // Context for global events is callbackContext if it is a DOM node or jQuery collection
403                         globalEventContext = s.context &&
404                                 ( callbackContext.nodeType || callbackContext.jquery ) ?
405                                         jQuery( callbackContext ) :
406                                         jQuery.event,
407                         // Deferreds
408                         deferred = jQuery.Deferred(),
409                         completeDeferred = jQuery.Callbacks("once memory"),
410                         // Status-dependent callbacks
411                         statusCode = s.statusCode || {},
412                         // Headers (they are sent all at once)
413                         requestHeaders = {},
414                         requestHeadersNames = {},
415                         // The jqXHR state
416                         state = 0,
417                         // Default abort message
418                         strAbort = "canceled",
419                         // Fake xhr
420                         jqXHR = {
421                                 readyState: 0,
423                                 // Builds headers hashtable if needed
424                                 getResponseHeader: function( key ) {
425                                         var match;
426                                         if ( state === 2 ) {
427                                                 if ( !responseHeaders ) {
428                                                         responseHeaders = {};
429                                                         while ( (match = rheaders.exec( responseHeadersString )) ) {
430                                                                 responseHeaders[ match[1].toLowerCase() ] = match[ 2 ];
431                                                         }
432                                                 }
433                                                 match = responseHeaders[ key.toLowerCase() ];
434                                         }
435                                         return match == null ? null : match;
436                                 },
438                                 // Raw string
439                                 getAllResponseHeaders: function() {
440                                         return state === 2 ? responseHeadersString : null;
441                                 },
443                                 // Caches the header
444                                 setRequestHeader: function( name, value ) {
445                                         var lname = name.toLowerCase();
446                                         if ( !state ) {
447                                                 name = requestHeadersNames[ lname ] = requestHeadersNames[ lname ] || name;
448                                                 requestHeaders[ name ] = value;
449                                         }
450                                         return this;
451                                 },
453                                 // Overrides response content-type header
454                                 overrideMimeType: function( type ) {
455                                         if ( !state ) {
456                                                 s.mimeType = type;
457                                         }
458                                         return this;
459                                 },
461                                 // Status-dependent callbacks
462                                 statusCode: function( map ) {
463                                         var code;
464                                         if ( map ) {
465                                                 if ( state < 2 ) {
466                                                         for ( code in map ) {
467                                                                 // Lazy-add the new callback in a way that preserves old ones
468                                                                 statusCode[ code ] = [ statusCode[ code ], map[ code ] ];
469                                                         }
470                                                 } else {
471                                                         // Execute the appropriate callbacks
472                                                         jqXHR.always( map[ jqXHR.status ] );
473                                                 }
474                                         }
475                                         return this;
476                                 },
478                                 // Cancel the request
479                                 abort: function( statusText ) {
480                                         var finalText = statusText || strAbort;
481                                         if ( transport ) {
482                                                 transport.abort( finalText );
483                                         }
484                                         done( 0, finalText );
485                                         return this;
486                                 }
487                         };
489                 // Attach deferreds
490                 deferred.promise( jqXHR );
492                 // Remove hash character (#7531: and string promotion)
493                 // Add protocol if not provided (prefilters might expect it)
494                 // Handle falsy url in the settings object (#10093: consistency with old signature)
495                 // We also use the url parameter if available
496                 s.url = ( ( url || s.url || location.href ) + "" ).replace( rhash, "" )
497                         .replace( rprotocol, location.protocol + "//" );
499                 // Alias method option to type as per ticket #12004
500                 s.type = options.method || options.type || s.method || s.type;
502                 // Extract dataTypes list
503                 s.dataTypes = jQuery.trim( s.dataType || "*" ).toLowerCase().match( rnotwhite ) || [ "" ];
505                 // A cross-domain request is in order when the origin doesn't match the current origin.
506                 if ( s.crossDomain == null ) {
507                         urlAnchor = document.createElement( "a" );
509                         // Support: IE8-11+
510                         // IE throws exception if url is malformed, e.g. http://example.com:80x/
511                         try {
512                                 urlAnchor.href = s.url;
513                                 // Support: IE8-11+
514                                 // Anchor's host property isn't correctly set when s.url is relative
515                                 urlAnchor.href = urlAnchor.href;
516                                 s.crossDomain = originAnchor.protocol + "//" + originAnchor.host !==
517                                         urlAnchor.protocol + "//" + urlAnchor.host;
518                         } catch ( e ) {
519                                 // If there is an error parsing the URL, assume it is crossDomain,
520                                 // it can be rejected by the transport if it is invalid
521                                 s.crossDomain = true;
522                         }
523                 }
525                 // Convert data if not already a string
526                 if ( s.data && s.processData && typeof s.data !== "string" ) {
527                         s.data = jQuery.param( s.data, s.traditional );
528                 }
530                 // Apply prefilters
531                 inspectPrefiltersOrTransports( prefilters, s, options, jqXHR );
533                 // If request was aborted inside a prefilter, stop there
534                 if ( state === 2 ) {
535                         return jqXHR;
536                 }
538                 // We can fire global events as of now if asked to
539                 // Don't fire events if jQuery.event is undefined in an AMD-usage scenario (#15118)
540                 fireGlobals = jQuery.event && s.global;
542                 // Watch for a new set of requests
543                 if ( fireGlobals && jQuery.active++ === 0 ) {
544                         jQuery.event.trigger("ajaxStart");
545                 }
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
555                 cacheURL = s.url;
557                 // More options handling for requests with no content
558                 if ( !s.hasContent ) {
560                         // If data is available, append data to url
561                         if ( s.data ) {
562                                 cacheURL = ( s.url += ( rquery.test( cacheURL ) ? "&" : "?" ) + s.data );
563                                 // #9682: remove data so that it's not used in an eventual retry
564                                 delete s.data;
565                         }
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++;
576                         }
577                 }
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 ] );
583                         }
584                         if ( jQuery.etag[ cacheURL ] ) {
585                                 jqXHR.setRequestHeader( "If-None-Match", jQuery.etag[ cacheURL ] );
586                         }
587                 }
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 );
592                 }
594                 // Set the Accepts header for the server, depending on the dataType
595                 jqXHR.setRequestHeader(
596                         "Accept",
597                         s.dataTypes[ 0 ] && s.accepts[ s.dataTypes[0] ] ?
598                                 s.accepts[ s.dataTypes[0] ] +
599                                         ( s.dataTypes[ 0 ] !== "*" ? ", " + allTypes + "; q=0.01" : "" ) :
600                                 s.accepts[ "*" ]
601                 );
603                 // Check for headers option
604                 for ( i in s.headers ) {
605                         jqXHR.setRequestHeader( i, s.headers[ i ] );
606                 }
608                 // Allow custom headers/mimetypes and early abort
609                 if ( s.beforeSend &&
610                         ( s.beforeSend.call( callbackContext, jqXHR, s ) === false || state === 2 ) ) {
612                         // Abort if not done already and return
613                         return jqXHR.abort();
614                 }
616                 // Aborting is no longer a cancellation
617                 strAbort = "abort";
619                 // Install callbacks on deferreds
620                 completeDeferred.add( s.complete );
621                 jqXHR.done( s.success );
622                 jqXHR.fail( s.error );
624                 // Get transport
625                 transport = inspectPrefiltersOrTransports( transports, s, options, jqXHR );
627                 // If no transport, we auto-abort
628                 if ( !transport ) {
629                         done( -1, "No Transport" );
630                 } else {
631                         jqXHR.readyState = 1;
633                         // Send global event
634                         if ( fireGlobals ) {
635                                 globalEventContext.trigger( "ajaxSend", [ jqXHR, s ] );
636                         }
638                         // If request was aborted inside ajaxSend, stop there
639                         if ( state === 2 ) {
640                                 return jqXHR;
641                         }
643                         // Timeout
644                         if ( s.async && s.timeout > 0 ) {
645                                 timeoutTimer = setTimeout(function() {
646                                         jqXHR.abort("timeout");
647                                 }, s.timeout );
648                         }
650                         try {
651                                 state = 1;
652                                 transport.send( requestHeaders, done );
653                         } catch ( e ) {
654                                 // Propagate exception as error if not done
655                                 if ( state < 2 ) {
656                                         done( -1, e );
657                                 // Simply rethrow otherwise
658                                 } else {
659                                         throw e;
660                                 }
661                         }
662                 }
664                 // Callback for when everything is done
665                 function done( status, nativeStatusText, responses, headers ) {
666                         var isSuccess, success, error, response, modified,
667                                 statusText = nativeStatusText;
669                         // Called once
670                         if ( state === 2 ) {
671                                 return;
672                         }
674                         // State is "done" now
675                         state = 2;
677                         // Clear timeout if it exists
678                         if ( timeoutTimer ) {
679                                 clearTimeout( timeoutTimer );
680                         }
682                         // Dereference transport for early garbage collection
683                         // (no matter how long the jqXHR object will be used)
684                         transport = undefined;
686                         // Cache response headers
687                         responseHeadersString = headers || "";
689                         // Set readyState
690                         jqXHR.readyState = status > 0 ? 4 : 0;
692                         // Determine if successful
693                         isSuccess = status >= 200 && status < 300 || status === 304;
695                         // Get response data
696                         if ( responses ) {
697                                 response = ajaxHandleResponses( s, jqXHR, responses );
698                         }
700                         // Convert no matter what (that way responseXXX fields are always set)
701                         response = ajaxConvert( s, response, jqXHR, isSuccess );
703                         // If successful, handle type chaining
704                         if ( isSuccess ) {
706                                 // Set the If-Modified-Since and/or If-None-Match header, if in ifModified mode.
707                                 if ( s.ifModified ) {
708                                         modified = jqXHR.getResponseHeader("Last-Modified");
709                                         if ( modified ) {
710                                                 jQuery.lastModified[ cacheURL ] = modified;
711                                         }
712                                         modified = jqXHR.getResponseHeader("etag");
713                                         if ( modified ) {
714                                                 jQuery.etag[ cacheURL ] = modified;
715                                         }
716                                 }
718                                 // if no content
719                                 if ( status === 204 || s.type === "HEAD" ) {
720                                         statusText = "nocontent";
722                                 // if not modified
723                                 } else if ( status === 304 ) {
724                                         statusText = "notmodified";
726                                 // If we have data, let's convert it
727                                 } else {
728                                         statusText = response.state;
729                                         success = response.data;
730                                         error = response.error;
731                                         isSuccess = !error;
732                                 }
733                         } else {
734                                 // Extract error from statusText and normalize for non-aborts
735                                 error = statusText;
736                                 if ( status || !statusText ) {
737                                         statusText = "error";
738                                         if ( status < 0 ) {
739                                                 status = 0;
740                                         }
741                                 }
742                         }
744                         // Set data for the fake xhr object
745                         jqXHR.status = status;
746                         jqXHR.statusText = ( nativeStatusText || statusText ) + "";
748                         // Success/Error
749                         if ( isSuccess ) {
750                                 deferred.resolveWith( callbackContext, [ success, statusText, jqXHR ] );
751                         } else {
752                                 deferred.rejectWith( callbackContext, [ jqXHR, statusText, error ] );
753                         }
755                         // Status-dependent callbacks
756                         jqXHR.statusCode( statusCode );
757                         statusCode = undefined;
759                         if ( fireGlobals ) {
760                                 globalEventContext.trigger( isSuccess ? "ajaxSuccess" : "ajaxError",
761                                         [ jqXHR, s, isSuccess ? success : error ] );
762                         }
764                         // Complete
765                         completeDeferred.fireWith( callbackContext, [ jqXHR, statusText ] );
767                         if ( fireGlobals ) {
768                                 globalEventContext.trigger( "ajaxComplete", [ jqXHR, s ] );
769                                 // Handle the global AJAX counter
770                                 if ( !( --jQuery.active ) ) {
771                                         jQuery.event.trigger("ajaxStop");
772                                 }
773                         }
774                 }
776                 return jqXHR;
777         },
779         getJSON: function( url, data, callback ) {
780                 return jQuery.get( url, data, callback, "json" );
781         },
783         getScript: function( url, callback ) {
784                 return jQuery.get( url, undefined, callback, "script" );
785         }
788 jQuery.each( [ "get", "post" ], function( i, method ) {
789         jQuery[ method ] = function( url, data, callback, type ) {
790                 // Shift arguments if data argument was omitted
791                 if ( jQuery.isFunction( data ) ) {
792                         type = type || callback;
793                         callback = data;
794                         data = undefined;
795                 }
797                 // The url can be an options object (which then must have .url)
798                 return jQuery.ajax( jQuery.extend({
799                         url: url,
800                         type: method,
801                         dataType: type,
802                         data: data,
803                         success: callback
804                 }, jQuery.isPlainObject( url ) && url ) );
805         };
808 return jQuery;