Brought back a husk method to represent evalScripts. I didn't realize so many plugins...
[jquery.git] / src / ajax / ajax.js
1 jQuery.fn.extend({
2
3         /**
4          * Load HTML from a remote file and inject it into the DOM, only if it's
5          * been modified by the server.
6          *
7          * @example $("#feeds").loadIfModified("feeds.html");
8          * @before <div id="feeds"></div>
9          * @result <div id="feeds"><b>45</b> feeds found.</div>
10          *
11          * @name loadIfModified
12          * @type jQuery
13          * @param String url The URL of the HTML file to load.
14          * @param Map params (optional) Key/value pairs that will be sent to the server.
15          * @param Function callback (optional) A function to be executed whenever the data is loaded (parameters: responseText, status and response itself).
16          * @cat Ajax
17          */
18         loadIfModified: function( url, params, callback ) {
19                 this.load( url, params, callback, 1 );
20         },
21
22         /**
23          * Load HTML from a remote file and inject it into the DOM.
24          *
25          * Note: Avoid to use this to load scripts, instead use $.getScript.
26          * IE strips script tags when there aren't any other characters in front of it.
27          *
28          * @example $("#feeds").load("feeds.html");
29          * @before <div id="feeds"></div>
30          * @result <div id="feeds"><b>45</b> feeds found.</div>
31          *
32          * @example $("#feeds").load("feeds.html",
33          *   {limit: 25},
34          *   function() { alert("The last 25 entries in the feed have been loaded"); }
35          * );
36          * @desc Same as above, but with an additional parameter
37          * and a callback that is executed when the data was loaded.
38          *
39          * @name load
40          * @type jQuery
41          * @param String url The URL of the HTML file to load.
42          * @param Object params (optional) A set of key/value pairs that will be sent as data to the server.
43          * @param Function callback (optional) A function to be executed whenever the data is loaded (parameters: responseText, status and response itself).
44          * @cat Ajax
45          */
46         load: function( url, params, callback, ifModified ) {
47                 if ( jQuery.isFunction( url ) )
48                         return this.bind("load", url);
49
50                 callback = callback || function(){};
51
52                 // Default to a GET request
53                 var type = "GET";
54
55                 // If the second parameter was provided
56                 if ( params )
57                         // If it's a function
58                         if ( jQuery.isFunction( params ) ) {
59                                 // We assume that it's the callback
60                                 callback = params;
61                                 params = null;
62
63                         // Otherwise, build a param string
64                         } else {
65                                 params = jQuery.param( params );
66                                 type = "POST";
67                         }
68
69                 var self = this;
70
71                 // Request the remote document
72                 jQuery.ajax({
73                         url: url,
74                         type: type,
75                         data: params,
76                         ifModified: ifModified,
77                         complete: function(res, status){
78                                 // If successful, inject the HTML into all the matched elements
79                                 if ( status == "success" || !ifModified && status == "notmodified" )
80                                         self.html(res.responseText);
81
82                                 // Add delay to account for Safari's delay in globalEval
83                                 setTimeout(function(){
84                                         self.each( callback, [res.responseText, status, res] );
85                                 }, 13);
86                         }
87                 });
88                 return this;
89         },
90
91         /**
92          * Serializes a set of input elements into a string of data.
93          * This will serialize all given elements.
94          *
95          * A serialization similar to the form submit of a browser is
96          * provided by the [http://www.malsup.com/jquery/form/ Form Plugin].
97          * It also takes multiple-selects 
98          * into account, while this method recognizes only a single option.
99          *
100          * @example $("input[@type=text]").serialize();
101          * @before <input type='text' name='name' value='John'/>
102          * <input type='text' name='location' value='Boston'/>
103          * @after name=John&amp;location=Boston
104          * @desc Serialize a selection of input elements to a string
105          *
106          * @name serialize
107          * @type String
108          * @cat Ajax
109          */
110         serialize: function() {
111                 return jQuery.param( this );
112         },
113
114         // DEPRECATED
115         // This method no longer does anything - all script evaluation is
116         // taken care of within the HTML injection methods.
117         evalScripts: function(){}
118
119 });
120
121 // Attach a bunch of functions for handling common AJAX events
122
123 /**
124  * Attach a function to be executed whenever an AJAX request begins
125  * and there is none already active.
126  *
127  * @example $("#loading").ajaxStart(function(){
128  *   $(this).show();
129  * });
130  * @desc Show a loading message whenever an AJAX request starts
131  * (and none is already active).
132  *
133  * @name ajaxStart
134  * @type jQuery
135  * @param Function callback The function to execute.
136  * @cat Ajax
137  */
138
139 /**
140  * Attach a function to be executed whenever all AJAX requests have ended.
141  *
142  * @example $("#loading").ajaxStop(function(){
143  *   $(this).hide();
144  * });
145  * @desc Hide a loading message after all the AJAX requests have stopped.
146  *
147  * @name ajaxStop
148  * @type jQuery
149  * @param Function callback The function to execute.
150  * @cat Ajax
151  */
152
153 /**
154  * Attach a function to be executed whenever an AJAX request completes.
155  *
156  * The XMLHttpRequest and settings used for that request are passed
157  * as arguments to the callback.
158  *
159  * @example $("#msg").ajaxComplete(function(request, settings){
160  *   $(this).append("<li>Request Complete.</li>");
161  * });
162  * @desc Show a message when an AJAX request completes.
163  *
164  * @name ajaxComplete
165  * @type jQuery
166  * @param Function callback The function to execute.
167  * @cat Ajax
168  */
169
170 /**
171  * Attach a function to be executed whenever an AJAX request completes
172  * successfully.
173  *
174  * The XMLHttpRequest and settings used for that request are passed
175  * as arguments to the callback.
176  *
177  * @example $("#msg").ajaxSuccess(function(request, settings){
178  *   $(this).append("<li>Successful Request!</li>");
179  * });
180  * @desc Show a message when an AJAX request completes successfully.
181  *
182  * @name ajaxSuccess
183  * @type jQuery
184  * @param Function callback The function to execute.
185  * @cat Ajax
186  */
187
188 /**
189  * Attach a function to be executed whenever an AJAX request fails.
190  *
191  * The XMLHttpRequest and settings used for that request are passed
192  * as arguments to the callback. A third argument, an exception object,
193  * is passed if an exception occured while processing the request.
194  *
195  * @example $("#msg").ajaxError(function(request, settings){
196  *   $(this).append("<li>Error requesting page " + settings.url + "</li>");
197  * });
198  * @desc Show a message when an AJAX request fails.
199  *
200  * @name ajaxError
201  * @type jQuery
202  * @param Function callback The function to execute.
203  * @cat Ajax
204  */
205  
206 /**
207  * Attach a function to be executed before an AJAX request is sent.
208  *
209  * The XMLHttpRequest and settings used for that request are passed
210  * as arguments to the callback.
211  *
212  * @example $("#msg").ajaxSend(function(request, settings){
213  *   $(this).append("<li>Starting request at " + settings.url + "</li>");
214  * });
215  * @desc Show a message before an AJAX request is sent.
216  *
217  * @name ajaxSend
218  * @type jQuery
219  * @param Function callback The function to execute.
220  * @cat Ajax
221  */
222 jQuery.each( "ajaxStart,ajaxStop,ajaxComplete,ajaxError,ajaxSuccess,ajaxSend".split(","), function(i,o){
223         jQuery.fn[o] = function(f){
224                 return this.bind(o, f);
225         };
226 });
227
228 jQuery.extend({
229
230         /**
231          * Load a remote page using an HTTP GET request.
232          *
233          * This is an easy way to send a simple GET request to a server
234          * without having to use the more complex $.ajax function. It
235          * allows a single callback function to be specified that will
236          * be executed when the request is complete (and only if the response
237          * has a successful response code). If you need to have both error
238          * and success callbacks, you may want to use $.ajax.
239          *
240          * @example $.get("test.cgi");
241          *
242          * @example $.get("test.cgi", { name: "John", time: "2pm" } );
243          *
244          * @example $.get("test.cgi", function(data){
245          *   alert("Data Loaded: " + data);
246          * });
247          *
248          * @example $.get("test.cgi",
249          *   { name: "John", time: "2pm" },
250          *   function(data){
251          *     alert("Data Loaded: " + data);
252          *   }
253          * );
254          *
255          * @name $.get
256          * @type XMLHttpRequest
257          * @param String url The URL of the page to load.
258          * @param Map params (optional) Key/value pairs that will be sent to the server.
259          * @param Function callback (optional) A function to be executed whenever the data is loaded successfully.
260          * @cat Ajax
261          */
262         get: function( url, data, callback, type, ifModified ) {
263                 // shift arguments if data argument was ommited
264                 if ( jQuery.isFunction( data ) ) {
265                         callback = data;
266                         data = null;
267                 }
268                 
269                 return jQuery.ajax({
270                         type: "GET",
271                         url: url,
272                         data: data,
273                         success: callback,
274                         dataType: type,
275                         ifModified: ifModified
276                 });
277         },
278
279         /**
280          * Load a remote page using an HTTP GET request, only if it hasn't
281          * been modified since it was last retrieved.
282          *
283          * @example $.getIfModified("test.html");
284          *
285          * @example $.getIfModified("test.html", { name: "John", time: "2pm" } );
286          *
287          * @example $.getIfModified("test.cgi", function(data){
288          *   alert("Data Loaded: " + data);
289          * });
290          *
291          * @example $.getifModified("test.cgi",
292          *   { name: "John", time: "2pm" },
293          *   function(data){
294          *     alert("Data Loaded: " + data);
295          *   }
296          * );
297          *
298          * @name $.getIfModified
299          * @type XMLHttpRequest
300          * @param String url The URL of the page to load.
301          * @param Map params (optional) Key/value pairs that will be sent to the server.
302          * @param Function callback (optional) A function to be executed whenever the data is loaded successfully.
303          * @cat Ajax
304          */
305         getIfModified: function( url, data, callback, type ) {
306                 return jQuery.get(url, data, callback, type, 1);
307         },
308
309         /**
310          * Loads, and executes, a remote JavaScript file using an HTTP GET request.
311          *
312          * Warning: Safari <= 2.0.x is unable to evaluate scripts in a global
313          * context synchronously. If you load functions via getScript, make sure
314          * to call them after a delay.
315          *
316          * @example $.getScript("test.js");
317          *
318          * @example $.getScript("test.js", function(){
319          *   alert("Script loaded and executed.");
320          * });
321          *
322          * @name $.getScript
323          * @type XMLHttpRequest
324          * @param String url The URL of the page to load.
325          * @param Function callback (optional) A function to be executed whenever the data is loaded successfully.
326          * @cat Ajax
327          */
328         getScript: function( url, callback ) {
329                 return jQuery.get(url, null, callback, "script");
330         },
331
332         /**
333          * Load JSON data using an HTTP GET request.
334          *
335          * @example $.getJSON("test.js", function(json){
336          *   alert("JSON Data: " + json.users[3].name);
337          * });
338          *
339          * @example $.getJSON("test.js",
340          *   { name: "John", time: "2pm" },
341          *   function(json){
342          *     alert("JSON Data: " + json.users[3].name);
343          *   }
344          * );
345          *
346          * @name $.getJSON
347          * @type XMLHttpRequest
348          * @param String url The URL of the page to load.
349          * @param Map params (optional) Key/value pairs that will be sent to the server.
350          * @param Function callback A function to be executed whenever the data is loaded successfully.
351          * @cat Ajax
352          */
353         getJSON: function( url, data, callback ) {
354                 return jQuery.get(url, data, callback, "json");
355         },
356
357         /**
358          * Load a remote page using an HTTP POST request.
359          *
360          * @example $.post("test.cgi");
361          *
362          * @example $.post("test.cgi", { name: "John", time: "2pm" } );
363          *
364          * @example $.post("test.cgi", function(data){
365          *   alert("Data Loaded: " + data);
366          * });
367          *
368          * @example $.post("test.cgi",
369          *   { name: "John", time: "2pm" },
370          *   function(data){
371          *     alert("Data Loaded: " + data);
372          *   }
373          * );
374          *
375          * @name $.post
376          * @type XMLHttpRequest
377          * @param String url The URL of the page to load.
378          * @param Map params (optional) Key/value pairs that will be sent to the server.
379          * @param Function callback (optional) A function to be executed whenever the data is loaded successfully.
380          * @cat Ajax
381          */
382         post: function( url, data, callback, type ) {
383                 if ( jQuery.isFunction( data ) ) {
384                         callback = data;
385                         data = {};
386                 }
387
388                 return jQuery.ajax({
389                         type: "POST",
390                         url: url,
391                         data: data,
392                         success: callback,
393                         dataType: type
394                 });
395         },
396
397         /**
398          * Set the timeout in milliseconds of all AJAX requests to a specific amount of time.
399          * This will make all future AJAX requests timeout after a specified amount
400          * of time.
401          *
402          * Set to null or 0 to disable timeouts (default).
403          *
404          * You can manually abort requests with the XMLHttpRequest's (returned by
405          * all ajax functions) abort() method.
406          *
407          * Deprecated. Use $.ajaxSetup instead.
408          *
409          * @example $.ajaxTimeout( 5000 );
410          * @desc Make all AJAX requests timeout after 5 seconds.
411          *
412          * @name $.ajaxTimeout
413          * @type undefined
414          * @param Number time How long before an AJAX request times out, in milliseconds.
415          * @cat Ajax
416          */
417         ajaxTimeout: function( timeout ) {
418                 jQuery.ajaxSettings.timeout = timeout;
419         },
420         
421         /**
422          * Setup global settings for AJAX requests.
423          *
424          * See $.ajax for a description of all available options.
425          *
426          * @example $.ajaxSetup( {
427          *   url: "/xmlhttp/",
428          *   global: false,
429          *   type: "POST"
430          * } );
431          * $.ajax({ data: myData });
432          * @desc Sets the defaults for AJAX requests to the url "/xmlhttp/",
433          * disables global handlers and uses POST instead of GET. The following
434          * AJAX requests then sends some data without having to set anything else.
435          *
436          * @name $.ajaxSetup
437          * @type undefined
438          * @param Map settings Key/value pairs to use for all AJAX requests
439          * @cat Ajax
440          */
441         ajaxSetup: function( settings ) {
442                 jQuery.extend( jQuery.ajaxSettings, settings );
443         },
444
445         ajaxSettings: {
446                 global: true,
447                 type: "GET",
448                 timeout: 0,
449                 contentType: "application/x-www-form-urlencoded",
450                 processData: true,
451                 async: true,
452                 data: null
453         },
454         
455         // Last-Modified header cache for next request
456         lastModified: {},
457
458         /**
459          * Load a remote page using an HTTP request.
460          *
461          * This is jQuery's low-level AJAX implementation. See $.get, $.post etc. for
462          * higher-level abstractions that are often easier to understand and use,
463          * but don't offer as much functionality (such as error callbacks).
464          *
465          * $.ajax() returns the XMLHttpRequest that it creates. In most cases you won't
466          * need that object to manipulate directly, but it is available if you need to
467          * abort the request manually.
468          *
469          * '''Note:''' If you specify the dataType option described below, make sure
470          * the server sends the correct MIME type in the response (eg. xml as "text/xml").
471          * Sending the wrong MIME type can lead to unexpected problems in your script.
472          * See [[Specifying the Data Type for AJAX Requests]] for more information.
473          *
474          * Supported datatypes are (see dataType option):
475          *
476          * "xml": Returns a XML document that can be processed via jQuery.
477          *
478          * "html": Returns HTML as plain text, included script tags are evaluated.
479          *
480          * "script": Evaluates the response as Javascript and returns it as plain text.
481          *
482          * "json": Evaluates the response as JSON and returns a Javascript Object
483          *
484          * $.ajax() takes one argument, an object of key/value pairs, that are
485          * used to initalize and handle the request. These are all the key/values that can
486          * be used:
487          *
488          * (String) url - The URL to request.
489          *
490          * (String) type - The type of request to make ("POST" or "GET"), default is "GET".
491          *
492          * (String) dataType - The type of data that you're expecting back from
493          * the server. No default: If the server sends xml, the responseXML, otherwise
494          * the responseText is passed to the success callback.
495          *
496          * (Boolean) ifModified - Allow the request to be successful only if the
497          * response has changed since the last request. This is done by checking the
498          * Last-Modified header. Default value is false, ignoring the header.
499          *
500          * (Number) timeout - Local timeout in milliseconds to override global timeout, eg. to give a
501          * single request a longer timeout while all others timeout after 1 second.
502          * See $.ajaxTimeout() for global timeouts.
503          *
504          * (Boolean) global - Whether to trigger global AJAX event handlers for
505          * this request, default is true. Set to false to prevent that global handlers
506          * like ajaxStart or ajaxStop are triggered.
507          *
508          * (Function) error - A function to be called if the request fails. The
509          * function gets passed tree arguments: The XMLHttpRequest object, a
510          * string describing the type of error that occurred and an optional
511          * exception object, if one occured.
512          *
513          * (Function) success - A function to be called if the request succeeds. The
514          * function gets passed one argument: The data returned from the server,
515          * formatted according to the 'dataType' parameter.
516          *
517          * (Function) complete - A function to be called when the request finishes. The
518          * function gets passed two arguments: The XMLHttpRequest object and a
519          * string describing the type of success of the request.
520          *
521          * (Object|String) data - Data to be sent to the server. Converted to a query
522          * string, if not already a string. Is appended to the url for GET-requests.
523          * See processData option to prevent this automatic processing.
524          *
525          * (String) contentType - When sending data to the server, use this content-type.
526          * Default is "application/x-www-form-urlencoded", which is fine for most cases.
527          *
528          * (Boolean) processData - By default, data passed in to the data option as an object
529          * other as string will be processed and transformed into a query string, fitting to
530          * the default content-type "application/x-www-form-urlencoded". If you want to send
531          * DOMDocuments, set this option to false.
532          *
533          * (Boolean) async - By default, all requests are sent asynchronous (set to true).
534          * If you need synchronous requests, set this option to false.
535          *
536          * (Function) beforeSend - A pre-callback to set custom headers etc., the
537          * XMLHttpRequest is passed as the only argument.
538          *
539          * @example $.ajax({
540          *   type: "GET",
541          *   url: "test.js",
542          *   dataType: "script"
543          * })
544          * @desc Load and execute a JavaScript file.
545          *
546          * @example $.ajax({
547          *   type: "POST",
548          *   url: "some.php",
549          *   data: "name=John&location=Boston",
550          *   success: function(msg){
551          *     alert( "Data Saved: " + msg );
552          *   }
553          * });
554          * @desc Save some data to the server and notify the user once its complete.
555          *
556          * @example var html = $.ajax({
557          *  url: "some.php",
558          *  async: false
559          * }).responseText;
560          * @desc Loads data synchronously. Blocks the browser while the requests is active.
561          * It is better to block user interaction by other means when synchronization is
562          * necessary.
563          *
564          * @example var xmlDocument = [create xml document];
565          * $.ajax({
566          *   url: "page.php",
567          *   processData: false,
568          *   data: xmlDocument,
569          *   success: handleResponse
570          * });
571          * @desc Sends an xml document as data to the server. By setting the processData
572          * option to false, the automatic conversion of data to strings is prevented.
573          * 
574          * @name $.ajax
575          * @type XMLHttpRequest
576          * @param Map properties Key/value pairs to initialize the request with.
577          * @cat Ajax
578          * @see ajaxSetup(Map)
579          */
580         ajax: function( s ) {
581                 // Extend the settings, but re-extend 's' so that it can be
582                 // checked again later (in the test suite, specifically)
583                 s = jQuery.extend(true, s, jQuery.extend(true, {}, jQuery.ajaxSettings, s));
584
585                 // if data available
586                 if ( s.data ) {
587                         // convert data if not already a string
588                         if ( s.processData && typeof s.data != "string" )
589                                 s.data = jQuery.param(s.data);
590
591                         // append data to url for get requests
592                         if ( s.type.toLowerCase() == "get" ) {
593                                 // "?" + data or "&" + data (in case there are already params)
594                                 s.url += (s.url.indexOf("?") > -1 ? "&" : "?") + s.data;
595
596                                 // IE likes to send both get and post data, prevent this
597                                 s.data = null;
598                         }
599                 }
600
601                 // Watch for a new set of requests
602                 if ( s.global && ! jQuery.active++ )
603                         jQuery.event.trigger( "ajaxStart" );
604
605                 var requestDone = false;
606
607                 // Create the request object; Microsoft failed to properly
608                 // implement the XMLHttpRequest in IE7, so we use the ActiveXObject when it is available
609                 var xml = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
610
611                 // Open the socket
612                 xml.open(s.type, s.url, s.async);
613
614                 // Set the correct header, if data is being sent
615                 if ( s.data )
616                         xml.setRequestHeader("Content-Type", s.contentType);
617
618                 // Set the If-Modified-Since header, if ifModified mode.
619                 if ( s.ifModified )
620                         xml.setRequestHeader("If-Modified-Since",
621                                 jQuery.lastModified[s.url] || "Thu, 01 Jan 1970 00:00:00 GMT" );
622
623                 // Set header so the called script knows that it's an XMLHttpRequest
624                 xml.setRequestHeader("X-Requested-With", "XMLHttpRequest");
625
626                 // Allow custom headers/mimetypes
627                 if( s.beforeSend )
628                         s.beforeSend(xml);
629                         
630                 if ( s.global )
631                     jQuery.event.trigger("ajaxSend", [xml, s]);
632
633                 // Wait for a response to come back
634                 var onreadystatechange = function(isTimeout){
635                         // The transfer is complete and the data is available, or the request timed out
636                         if ( !requestDone && xml && (xml.readyState == 4 || isTimeout == "timeout") ) {
637                                 requestDone = true;
638                                 
639                                 // clear poll interval
640                                 if (ival) {
641                                         clearInterval(ival);
642                                         ival = null;
643                                 }
644                                 
645                                 var status = isTimeout == "timeout" && "timeout" ||
646                                         !jQuery.httpSuccess( xml ) && "error" ||
647                                         s.ifModified && jQuery.httpNotModified( xml, s.url ) && "notmodified" ||
648                                         "success";
649
650                                 if ( status == "success" ) {
651                                         // Watch for, and catch, XML document parse errors
652                                         try {
653                                                 // process the data (runs the xml through httpData regardless of callback)
654                                                 var data = jQuery.httpData( xml, s.dataType );
655                                         } catch(e) {
656                                                 status = "parsererror";
657                                         }
658                                 }
659
660                                 // Make sure that the request was successful or notmodified
661                                 if ( status == "success" ) {
662                                         // Cache Last-Modified header, if ifModified mode.
663                                         var modRes;
664                                         try {
665                                                 modRes = xml.getResponseHeader("Last-Modified");
666                                         } catch(e) {} // swallow exception thrown by FF if header is not available
667         
668                                         if ( s.ifModified && modRes )
669                                                 jQuery.lastModified[s.url] = modRes;
670         
671                                         // If a local callback was specified, fire it and pass it the data
672                                         if ( s.success )
673                                                 s.success( data, status );
674         
675                                         // Fire the global callback
676                                         if ( s.global )
677                                                 jQuery.event.trigger( "ajaxSuccess", [xml, s] );
678                                 } else
679                                         jQuery.handleError(s, xml, status);
680
681                                 // The request was completed
682                                 if( s.global )
683                                         jQuery.event.trigger( "ajaxComplete", [xml, s] );
684
685                                 // Handle the global AJAX counter
686                                 if ( s.global && ! --jQuery.active )
687                                         jQuery.event.trigger( "ajaxStop" );
688
689                                 // Process result
690                                 if ( s.complete )
691                                         s.complete(xml, status);
692
693                                 // Stop memory leaks
694                                 if(s.async)
695                                         xml = null;
696                         }
697                 };
698                 
699                 if ( s.async ) {
700                         // don't attach the handler to the request, just poll it instead
701                         var ival = setInterval(onreadystatechange, 13); 
702
703                         // Timeout checker
704                         if ( s.timeout > 0 )
705                                 setTimeout(function(){
706                                         // Check to see if the request is still happening
707                                         if ( xml ) {
708                                                 // Cancel the request
709                                                 xml.abort();
710         
711                                                 if( !requestDone )
712                                                         onreadystatechange( "timeout" );
713                                         }
714                                 }, s.timeout);
715                 }
716                         
717                 // Send the data
718                 try {
719                         xml.send(s.data);
720                 } catch(e) {
721                         jQuery.handleError(s, xml, null, e);
722                 }
723                 
724                 // firefox 1.5 doesn't fire statechange for sync requests
725                 if ( !s.async )
726                         onreadystatechange();
727                 
728                 // return XMLHttpRequest to allow aborting the request etc.
729                 return xml;
730         },
731
732         handleError: function( s, xml, status, e ) {
733                 // If a local callback was specified, fire it
734                 if ( s.error ) s.error( xml, status, e );
735
736                 // Fire the global callback
737                 if ( s.global )
738                         jQuery.event.trigger( "ajaxError", [xml, s, e] );
739         },
740
741         // Counter for holding the number of active queries
742         active: 0,
743
744         // Determines if an XMLHttpRequest was successful or not
745         httpSuccess: function( r ) {
746                 try {
747                         return !r.status && location.protocol == "file:" ||
748                                 ( r.status >= 200 && r.status < 300 ) || r.status == 304 ||
749                                 jQuery.browser.safari && r.status == undefined;
750                 } catch(e){}
751                 return false;
752         },
753
754         // Determines if an XMLHttpRequest returns NotModified
755         httpNotModified: function( xml, url ) {
756                 try {
757                         var xmlRes = xml.getResponseHeader("Last-Modified");
758
759                         // Firefox always returns 200. check Last-Modified date
760                         return xml.status == 304 || xmlRes == jQuery.lastModified[url] ||
761                                 jQuery.browser.safari && xml.status == undefined;
762                 } catch(e){}
763                 return false;
764         },
765
766         /* Get the data out of an XMLHttpRequest.
767          * Return parsed XML if content-type header is "xml" and type is "xml" or omitted,
768          * otherwise return plain text.
769          * (String) data - The type of data that you're expecting back,
770          * (e.g. "xml", "html", "script")
771          */
772         httpData: function( r, type ) {
773                 var ct = r.getResponseHeader("content-type");
774                 var xml = type == "xml" || !type && ct && ct.indexOf("xml") >= 0;
775                 data = xml ? r.responseXML : r.responseText;
776
777                 if ( xml && data.documentElement.tagName == "parsererror" )
778                         throw "parsererror";
779
780                 // If the type is "script", eval it in global context
781                 if ( type == "script" )
782                         jQuery.globalEval( data );
783
784                 // Get the JavaScript object, if JSON is used.
785                 if ( type == "json" )
786                         data = eval("(" + data + ")");
787
788                 return data;
789         },
790
791         // Serialize an array of form elements or a set of
792         // key/values into a query string
793         param: function( a ) {
794                 var s = [];
795
796                 // If an array was passed in, assume that it is an array
797                 // of form elements
798                 if ( a.constructor == Array || a.jquery )
799                         // Serialize the form elements
800                         jQuery.each( a, function(){
801                                 s.push( encodeURIComponent(this.name) + "=" + encodeURIComponent( this.value ) );
802                         });
803
804                 // Otherwise, assume that it's an object of key/value pairs
805                 else
806                         // Serialize the key/values
807                         for ( var j in a )
808                                 // If the value is an array then the key names need to be repeated
809                                 if ( a[j] && a[j].constructor == Array )
810                                         jQuery.each( a[j], function(){
811                                                 s.push( encodeURIComponent(j) + "=" + encodeURIComponent( this ) );
812                                         });
813                                 else
814                                         s.push( encodeURIComponent(j) + "=" + encodeURIComponent( a[j] ) );
815
816                 // Return the resulting serialization
817                 return s.join("&");
818         }
819
820 });