Build: update node dependencies barring jscs
[jquery.git] / src / ajax.js
blobcaffe883df4199447f1226b3826966f46c336cbf
1 define([
2         "./core",
3         "./var/rnotwhite",
4         "./ajax/var/nonce",
5         "./ajax/var/rquery",
6         "./core/init",
7         "./ajax/parseJSON",
8         "./ajax/parseXML",
9         "./deferred"
10 ], function( jQuery, rnotwhite, nonce, rquery ) {
12 var
13         // Document location
14         ajaxLocParts,
15         ajaxLocation,
17         rhash = /#.*$/,
18         rts = /([?&])_=[^&]*/,
19         rheaders = /^(.*?):[ \t]*([^\r\n]*)\r?$/mg, // IE leaves an \r character at EOL
20         // #7653, #8125, #8152: local protocol detection
21         rlocalProtocol = /^(?:about|app|app-storage|.+-extension|file|res|widget):$/,
22         rnoContent = /^(?:GET|HEAD)$/,
23         rprotocol = /^\/\//,
24         rurl = /^([\w.+-]+:)(?:\/\/(?:[^\/?#]*@|)([^\/?#:]*)(?::(\d+)|)|)/,
26         /* Prefilters
27          * 1) They are useful to introduce custom dataTypes (see ajax/jsonp.js for an example)
28          * 2) These are called:
29          *    - BEFORE asking for a transport
30          *    - AFTER param serialization (s.data is a string if s.processData is true)
31          * 3) key is the dataType
32          * 4) the catchall symbol "*" can be used
33          * 5) execution will start with transport dataType and THEN continue down to "*" if needed
34          */
35         prefilters = {},
37         /* Transports bindings
38          * 1) key is the dataType
39          * 2) the catchall symbol "*" can be used
40          * 3) selection will start with transport dataType and THEN go to "*" if needed
41          */
42         transports = {},
44         // Avoid comment-prolog char sequence (#10098); must appease lint and evade compression
45         allTypes = "*/".concat("*");
47 // Support: IE<8
48 // #8138, IE may throw an exception when accessing
49 // a field from window.location if document.domain has been set
50 try {
51         ajaxLocation = location.href;
52 } catch( e ) {
53         // Use the href attribute of an A element
54         // since IE will modify it given document.location
55         ajaxLocation = document.createElement( "a" );
56         ajaxLocation.href = "";
57         ajaxLocation = ajaxLocation.href;
60 // Segment location into parts
61 ajaxLocParts = rurl.exec( ajaxLocation.toLowerCase() ) || [];
63 // Base "constructor" for jQuery.ajaxPrefilter and jQuery.ajaxTransport
64 function addToPrefiltersOrTransports( structure ) {
66         // dataTypeExpression is optional and defaults to "*"
67         return function( dataTypeExpression, func ) {
69                 if ( typeof dataTypeExpression !== "string" ) {
70                         func = dataTypeExpression;
71                         dataTypeExpression = "*";
72                 }
74                 var dataType,
75                         i = 0,
76                         dataTypes = dataTypeExpression.toLowerCase().match( rnotwhite ) || [];
78                 if ( jQuery.isFunction( func ) ) {
79                         // For each dataType in the dataTypeExpression
80                         while ( (dataType = dataTypes[i++]) ) {
81                                 // Prepend if requested
82                                 if ( dataType.charAt( 0 ) === "+" ) {
83                                         dataType = dataType.slice( 1 ) || "*";
84                                         (structure[ dataType ] = structure[ dataType ] || []).unshift( func );
86                                 // Otherwise append
87                                 } else {
88                                         (structure[ dataType ] = structure[ dataType ] || []).push( func );
89                                 }
90                         }
91                 }
92         };
95 // Base inspection function for prefilters and transports
96 function inspectPrefiltersOrTransports( structure, options, originalOptions, jqXHR ) {
98         var inspected = {},
99                 seekingTransport = ( structure === transports );
101         function inspect( dataType ) {
102                 var selected;
103                 inspected[ dataType ] = true;
104                 jQuery.each( structure[ dataType ] || [], function( _, prefilterOrFactory ) {
105                         var dataTypeOrTransport = prefilterOrFactory( options, originalOptions, jqXHR );
106                         if ( typeof dataTypeOrTransport === "string" && !seekingTransport && !inspected[ dataTypeOrTransport ] ) {
107                                 options.dataTypes.unshift( dataTypeOrTransport );
108                                 inspect( dataTypeOrTransport );
109                                 return false;
110                         } else if ( seekingTransport ) {
111                                 return !( selected = dataTypeOrTransport );
112                         }
113                 });
114                 return selected;
115         }
117         return inspect( options.dataTypes[ 0 ] ) || !inspected[ "*" ] && inspect( "*" );
120 // A special extend for ajax options
121 // that takes "flat" options (not to be deep extended)
122 // Fixes #9887
123 function ajaxExtend( target, src ) {
124         var deep, key,
125                 flatOptions = jQuery.ajaxSettings.flatOptions || {};
127         for ( key in src ) {
128                 if ( src[ key ] !== undefined ) {
129                         ( flatOptions[ key ] ? target : ( deep || (deep = {}) ) )[ key ] = src[ key ];
130                 }
131         }
132         if ( deep ) {
133                 jQuery.extend( true, target, deep );
134         }
136         return target;
139 /* Handles responses to an ajax request:
140  * - finds the right dataType (mediates between content-type and expected dataType)
141  * - returns the corresponding response
142  */
143 function ajaxHandleResponses( s, jqXHR, responses ) {
144         var firstDataType, ct, finalDataType, type,
145                 contents = s.contents,
146                 dataTypes = s.dataTypes;
148         // Remove auto dataType and get content-type in the process
149         while ( dataTypes[ 0 ] === "*" ) {
150                 dataTypes.shift();
151                 if ( ct === undefined ) {
152                         ct = s.mimeType || jqXHR.getResponseHeader("Content-Type");
153                 }
154         }
156         // Check if we're dealing with a known content-type
157         if ( ct ) {
158                 for ( type in contents ) {
159                         if ( contents[ type ] && contents[ type ].test( ct ) ) {
160                                 dataTypes.unshift( type );
161                                 break;
162                         }
163                 }
164         }
166         // Check to see if we have a response for the expected dataType
167         if ( dataTypes[ 0 ] in responses ) {
168                 finalDataType = dataTypes[ 0 ];
169         } else {
170                 // Try convertible dataTypes
171                 for ( type in responses ) {
172                         if ( !dataTypes[ 0 ] || s.converters[ type + " " + dataTypes[0] ] ) {
173                                 finalDataType = type;
174                                 break;
175                         }
176                         if ( !firstDataType ) {
177                                 firstDataType = type;
178                         }
179                 }
180                 // Or just use first one
181                 finalDataType = finalDataType || firstDataType;
182         }
184         // If we found a dataType
185         // We add the dataType to the list if needed
186         // and return the corresponding response
187         if ( finalDataType ) {
188                 if ( finalDataType !== dataTypes[ 0 ] ) {
189                         dataTypes.unshift( finalDataType );
190                 }
191                 return responses[ finalDataType ];
192         }
195 /* Chain conversions given the request and the original response
196  * Also sets the responseXXX fields on the jqXHR instance
197  */
198 function ajaxConvert( s, response, jqXHR, isSuccess ) {
199         var conv2, current, conv, tmp, prev,
200                 converters = {},
201                 // Work with a copy of dataTypes in case we need to modify it for conversion
202                 dataTypes = s.dataTypes.slice();
204         // Create converters map with lowercased keys
205         if ( dataTypes[ 1 ] ) {
206                 for ( conv in s.converters ) {
207                         converters[ conv.toLowerCase() ] = s.converters[ conv ];
208                 }
209         }
211         current = dataTypes.shift();
213         // Convert to each sequential dataType
214         while ( current ) {
216                 if ( s.responseFields[ current ] ) {
217                         jqXHR[ s.responseFields[ current ] ] = response;
218                 }
220                 // Apply the dataFilter if provided
221                 if ( !prev && isSuccess && s.dataFilter ) {
222                         response = s.dataFilter( response, s.dataType );
223                 }
225                 prev = current;
226                 current = dataTypes.shift();
228                 if ( current ) {
230                         // There's only work to do if current dataType is non-auto
231                         if ( current === "*" ) {
233                                 current = prev;
235                         // Convert response if prev dataType is non-auto and differs from current
236                         } else if ( prev !== "*" && prev !== current ) {
238                                 // Seek a direct converter
239                                 conv = converters[ prev + " " + current ] || converters[ "* " + current ];
241                                 // If none found, seek a pair
242                                 if ( !conv ) {
243                                         for ( conv2 in converters ) {
245                                                 // If conv2 outputs current
246                                                 tmp = conv2.split( " " );
247                                                 if ( tmp[ 1 ] === current ) {
249                                                         // If prev can be converted to accepted input
250                                                         conv = converters[ prev + " " + tmp[ 0 ] ] ||
251                                                                 converters[ "* " + tmp[ 0 ] ];
252                                                         if ( conv ) {
253                                                                 // Condense equivalence converters
254                                                                 if ( conv === true ) {
255                                                                         conv = converters[ conv2 ];
257                                                                 // Otherwise, insert the intermediate dataType
258                                                                 } else if ( converters[ conv2 ] !== true ) {
259                                                                         current = tmp[ 0 ];
260                                                                         dataTypes.unshift( tmp[ 1 ] );
261                                                                 }
262                                                                 break;
263                                                         }
264                                                 }
265                                         }
266                                 }
268                                 // Apply converter (if not an equivalence)
269                                 if ( conv !== true ) {
271                                         // Unless errors are allowed to bubble, catch and return them
272                                         if ( conv && s[ "throws" ] ) {
273                                                 response = conv( response );
274                                         } else {
275                                                 try {
276                                                         response = conv( response );
277                                                 } catch ( e ) {
278                                                         return { state: "parsererror", error: conv ? e : "No conversion from " + prev + " to " + current };
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: ajaxLocation,
300                 type: "GET",
301                 isLocal: rlocalProtocol.test( ajaxLocParts[ 1 ] ),
302                 global: true,
303                 processData: true,
304                 async: true,
305                 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: /xml/,
328                         html: /html/,
329                         json: /json/
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": jQuery.parseJSON,
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 // Cross-domain detection vars
394                         parts,
395                         // Loop variable
396                         i,
397                         // URL without anti-cache param
398                         cacheURL,
399                         // Response headers as string
400                         responseHeadersString,
401                         // timeout handle
402                         timeoutTimer,
404                         // To know if global events are to be dispatched
405                         fireGlobals,
407                         transport,
408                         // Response headers
409                         responseHeaders,
410                         // Create the final options object
411                         s = jQuery.ajaxSetup( {}, options ),
412                         // Callbacks context
413                         callbackContext = s.context || s,
414                         // Context for global events is callbackContext if it is a DOM node or jQuery collection
415                         globalEventContext = s.context && ( callbackContext.nodeType || callbackContext.jquery ) ?
416                                 jQuery( callbackContext ) :
417                                 jQuery.event,
418                         // Deferreds
419                         deferred = jQuery.Deferred(),
420                         completeDeferred = jQuery.Callbacks("once memory"),
421                         // Status-dependent callbacks
422                         statusCode = s.statusCode || {},
423                         // Headers (they are sent all at once)
424                         requestHeaders = {},
425                         requestHeadersNames = {},
426                         // The jqXHR state
427                         state = 0,
428                         // Default abort message
429                         strAbort = "canceled",
430                         // Fake xhr
431                         jqXHR = {
432                                 readyState: 0,
434                                 // Builds headers hashtable if needed
435                                 getResponseHeader: function( key ) {
436                                         var match;
437                                         if ( state === 2 ) {
438                                                 if ( !responseHeaders ) {
439                                                         responseHeaders = {};
440                                                         while ( (match = rheaders.exec( responseHeadersString )) ) {
441                                                                 responseHeaders[ match[1].toLowerCase() ] = match[ 2 ];
442                                                         }
443                                                 }
444                                                 match = responseHeaders[ key.toLowerCase() ];
445                                         }
446                                         return match == null ? null : match;
447                                 },
449                                 // Raw string
450                                 getAllResponseHeaders: function() {
451                                         return state === 2 ? responseHeadersString : null;
452                                 },
454                                 // Caches the header
455                                 setRequestHeader: function( name, value ) {
456                                         var lname = name.toLowerCase();
457                                         if ( !state ) {
458                                                 name = requestHeadersNames[ lname ] = requestHeadersNames[ lname ] || name;
459                                                 requestHeaders[ name ] = value;
460                                         }
461                                         return this;
462                                 },
464                                 // Overrides response content-type header
465                                 overrideMimeType: function( type ) {
466                                         if ( !state ) {
467                                                 s.mimeType = type;
468                                         }
469                                         return this;
470                                 },
472                                 // Status-dependent callbacks
473                                 statusCode: function( map ) {
474                                         var code;
475                                         if ( map ) {
476                                                 if ( state < 2 ) {
477                                                         for ( code in map ) {
478                                                                 // Lazy-add the new callback in a way that preserves old ones
479                                                                 statusCode[ code ] = [ statusCode[ code ], map[ code ] ];
480                                                         }
481                                                 } else {
482                                                         // Execute the appropriate callbacks
483                                                         jqXHR.always( map[ jqXHR.status ] );
484                                                 }
485                                         }
486                                         return this;
487                                 },
489                                 // Cancel the request
490                                 abort: function( statusText ) {
491                                         var finalText = statusText || strAbort;
492                                         if ( transport ) {
493                                                 transport.abort( finalText );
494                                         }
495                                         done( 0, finalText );
496                                         return this;
497                                 }
498                         };
500                 // Attach deferreds
501                 deferred.promise( jqXHR ).complete = completeDeferred.add;
502                 jqXHR.success = jqXHR.done;
503                 jqXHR.error = jqXHR.fail;
505                 // Remove hash character (#7531: and string promotion)
506                 // Add protocol if not provided (#5866: IE7 issue with protocol-less urls)
507                 // Handle falsy url in the settings object (#10093: consistency with old signature)
508                 // We also use the url parameter if available
509                 s.url = ( ( url || s.url || ajaxLocation ) + "" ).replace( rhash, "" ).replace( rprotocol, ajaxLocParts[ 1 ] + "//" );
511                 // Alias method option to type as per ticket #12004
512                 s.type = options.method || options.type || s.method || s.type;
514                 // Extract dataTypes list
515                 s.dataTypes = jQuery.trim( s.dataType || "*" ).toLowerCase().match( rnotwhite ) || [ "" ];
517                 // A cross-domain request is in order when we have a protocol:host:port mismatch
518                 if ( s.crossDomain == null ) {
519                         parts = rurl.exec( s.url.toLowerCase() );
520                         s.crossDomain = !!( parts &&
521                                 ( parts[ 1 ] !== ajaxLocParts[ 1 ] || parts[ 2 ] !== ajaxLocParts[ 2 ] ||
522                                         ( parts[ 3 ] || ( parts[ 1 ] === "http:" ? "80" : "443" ) ) !==
523                                                 ( ajaxLocParts[ 3 ] || ( ajaxLocParts[ 1 ] === "http:" ? "80" : "443" ) ) )
524                         );
525                 }
527                 // Convert data if not already a string
528                 if ( s.data && s.processData && typeof s.data !== "string" ) {
529                         s.data = jQuery.param( s.data, s.traditional );
530                 }
532                 // Apply prefilters
533                 inspectPrefiltersOrTransports( prefilters, s, options, jqXHR );
535                 // If request was aborted inside a prefilter, stop there
536                 if ( state === 2 ) {
537                         return jqXHR;
538                 }
540                 // We can fire global events as of now if asked to
541                 // Don't fire events if jQuery.event is undefined in an AMD-usage scenario (#15118)
542                 fireGlobals = jQuery.event && s.global;
544                 // Watch for a new set of requests
545                 if ( fireGlobals && jQuery.active++ === 0 ) {
546                         jQuery.event.trigger("ajaxStart");
547                 }
549                 // Uppercase the type
550                 s.type = s.type.toUpperCase();
552                 // Determine if request has content
553                 s.hasContent = !rnoContent.test( s.type );
555                 // Save the URL in case we're toying with the If-Modified-Since
556                 // and/or If-None-Match header later on
557                 cacheURL = s.url;
559                 // More options handling for requests with no content
560                 if ( !s.hasContent ) {
562                         // If data is available, append data to url
563                         if ( s.data ) {
564                                 cacheURL = ( s.url += ( rquery.test( cacheURL ) ? "&" : "?" ) + s.data );
565                                 // #9682: remove data so that it's not used in an eventual retry
566                                 delete s.data;
567                         }
569                         // Add anti-cache in url if needed
570                         if ( s.cache === false ) {
571                                 s.url = rts.test( cacheURL ) ?
573                                         // If there is already a '_' parameter, set its value
574                                         cacheURL.replace( rts, "$1_=" + nonce++ ) :
576                                         // Otherwise add one to the end
577                                         cacheURL + ( rquery.test( cacheURL ) ? "&" : "?" ) + "_=" + nonce++;
578                         }
579                 }
581                 // Set the If-Modified-Since and/or If-None-Match header, if in ifModified mode.
582                 if ( s.ifModified ) {
583                         if ( jQuery.lastModified[ cacheURL ] ) {
584                                 jqXHR.setRequestHeader( "If-Modified-Since", jQuery.lastModified[ cacheURL ] );
585                         }
586                         if ( jQuery.etag[ cacheURL ] ) {
587                                 jqXHR.setRequestHeader( "If-None-Match", jQuery.etag[ cacheURL ] );
588                         }
589                 }
591                 // Set the correct header, if data is being sent
592                 if ( s.data && s.hasContent && s.contentType !== false || options.contentType ) {
593                         jqXHR.setRequestHeader( "Content-Type", s.contentType );
594                 }
596                 // Set the Accepts header for the server, depending on the dataType
597                 jqXHR.setRequestHeader(
598                         "Accept",
599                         s.dataTypes[ 0 ] && s.accepts[ s.dataTypes[0] ] ?
600                                 s.accepts[ s.dataTypes[0] ] + ( s.dataTypes[ 0 ] !== "*" ? ", " + allTypes + "; q=0.01" : "" ) :
601                                 s.accepts[ "*" ]
602                 );
604                 // Check for headers option
605                 for ( i in s.headers ) {
606                         jqXHR.setRequestHeader( i, s.headers[ i ] );
607                 }
609                 // Allow custom headers/mimetypes and early abort
610                 if ( s.beforeSend && ( s.beforeSend.call( callbackContext, jqXHR, s ) === false || state === 2 ) ) {
611                         // Abort if not done already and return
612                         return jqXHR.abort();
613                 }
615                 // aborting is no longer a cancellation
616                 strAbort = "abort";
618                 // Install callbacks on deferreds
619                 for ( i in { success: 1, error: 1, complete: 1 } ) {
620                         jqXHR[ i ]( s[ i ] );
621                 }
623                 // Get transport
624                 transport = inspectPrefiltersOrTransports( transports, s, options, jqXHR );
626                 // If no transport, we auto-abort
627                 if ( !transport ) {
628                         done( -1, "No Transport" );
629                 } else {
630                         jqXHR.readyState = 1;
632                         // Send global event
633                         if ( fireGlobals ) {
634                                 globalEventContext.trigger( "ajaxSend", [ jqXHR, s ] );
635                         }
636                         // Timeout
637                         if ( s.async && s.timeout > 0 ) {
638                                 timeoutTimer = setTimeout(function() {
639                                         jqXHR.abort("timeout");
640                                 }, s.timeout );
641                         }
643                         try {
644                                 state = 1;
645                                 transport.send( requestHeaders, done );
646                         } catch ( e ) {
647                                 // Propagate exception as error if not done
648                                 if ( state < 2 ) {
649                                         done( -1, e );
650                                 // Simply rethrow otherwise
651                                 } else {
652                                         throw e;
653                                 }
654                         }
655                 }
657                 // Callback for when everything is done
658                 function done( status, nativeStatusText, responses, headers ) {
659                         var isSuccess, success, error, response, modified,
660                                 statusText = nativeStatusText;
662                         // Called once
663                         if ( state === 2 ) {
664                                 return;
665                         }
667                         // State is "done" now
668                         state = 2;
670                         // Clear timeout if it exists
671                         if ( timeoutTimer ) {
672                                 clearTimeout( timeoutTimer );
673                         }
675                         // Dereference transport for early garbage collection
676                         // (no matter how long the jqXHR object will be used)
677                         transport = undefined;
679                         // Cache response headers
680                         responseHeadersString = headers || "";
682                         // Set readyState
683                         jqXHR.readyState = status > 0 ? 4 : 0;
685                         // Determine if successful
686                         isSuccess = status >= 200 && status < 300 || status === 304;
688                         // Get response data
689                         if ( responses ) {
690                                 response = ajaxHandleResponses( s, jqXHR, responses );
691                         }
693                         // Convert no matter what (that way responseXXX fields are always set)
694                         response = ajaxConvert( s, response, jqXHR, isSuccess );
696                         // If successful, handle type chaining
697                         if ( isSuccess ) {
699                                 // Set the If-Modified-Since and/or If-None-Match header, if in ifModified mode.
700                                 if ( s.ifModified ) {
701                                         modified = jqXHR.getResponseHeader("Last-Modified");
702                                         if ( modified ) {
703                                                 jQuery.lastModified[ cacheURL ] = modified;
704                                         }
705                                         modified = jqXHR.getResponseHeader("etag");
706                                         if ( modified ) {
707                                                 jQuery.etag[ cacheURL ] = modified;
708                                         }
709                                 }
711                                 // if no content
712                                 if ( status === 204 || s.type === "HEAD" ) {
713                                         statusText = "nocontent";
715                                 // if not modified
716                                 } else if ( status === 304 ) {
717                                         statusText = "notmodified";
719                                 // If we have data, let's convert it
720                                 } else {
721                                         statusText = response.state;
722                                         success = response.data;
723                                         error = response.error;
724                                         isSuccess = !error;
725                                 }
726                         } else {
727                                 // We extract error from statusText
728                                 // then normalize statusText and status for non-aborts
729                                 error = statusText;
730                                 if ( status || !statusText ) {
731                                         statusText = "error";
732                                         if ( status < 0 ) {
733                                                 status = 0;
734                                         }
735                                 }
736                         }
738                         // Set data for the fake xhr object
739                         jqXHR.status = status;
740                         jqXHR.statusText = ( nativeStatusText || statusText ) + "";
742                         // Success/Error
743                         if ( isSuccess ) {
744                                 deferred.resolveWith( callbackContext, [ success, statusText, jqXHR ] );
745                         } else {
746                                 deferred.rejectWith( callbackContext, [ jqXHR, statusText, error ] );
747                         }
749                         // Status-dependent callbacks
750                         jqXHR.statusCode( statusCode );
751                         statusCode = undefined;
753                         if ( fireGlobals ) {
754                                 globalEventContext.trigger( isSuccess ? "ajaxSuccess" : "ajaxError",
755                                         [ jqXHR, s, isSuccess ? success : error ] );
756                         }
758                         // Complete
759                         completeDeferred.fireWith( callbackContext, [ jqXHR, statusText ] );
761                         if ( fireGlobals ) {
762                                 globalEventContext.trigger( "ajaxComplete", [ jqXHR, s ] );
763                                 // Handle the global AJAX counter
764                                 if ( !( --jQuery.active ) ) {
765                                         jQuery.event.trigger("ajaxStop");
766                                 }
767                         }
768                 }
770                 return jqXHR;
771         },
773         getJSON: function( url, data, callback ) {
774                 return jQuery.get( url, data, callback, "json" );
775         },
777         getScript: function( url, callback ) {
778                 return jQuery.get( url, undefined, callback, "script" );
779         }
782 jQuery.each( [ "get", "post" ], function( i, method ) {
783         jQuery[ method ] = function( url, data, callback, type ) {
784                 // shift arguments if data argument was omitted
785                 if ( jQuery.isFunction( data ) ) {
786                         type = type || callback;
787                         callback = data;
788                         data = undefined;
789                 }
791                 return jQuery.ajax({
792                         url: url,
793                         type: method,
794                         dataType: type,
795                         data: data,
796                         success: callback
797                 });
798         };
801 return jQuery;