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