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