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