Fixed bug #165 (ignoring the exception) and #156 (ifModified option added to $.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 Hash params A set of key/value pairs that will be sent to the server.
15          * @param Function callback A function to be executed whenever the data is loaded.
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          * @example $("#feeds").load("feeds.html")
26          * @before <div id="feeds"></div>
27          * @result <div id="feeds"><b>45</b> feeds found.</div>
28          *
29          * @example $("#feeds").load("feeds.html",
30          *   {test: true},
31          *   function() { alert("load is done"); }
32          * );
33          * @desc Same as above, but with an additional parameter
34          * and a callback that is executed when the data was loaded.
35          *
36          * @test stop();
37          * $('#first').load("data/name.php", function() {
38          *      ok( $('#first').text() == 'ERROR', 'Check if content was injected into the DOM' );
39          *      start();
40          * });
41          *
42          * @name load
43          * @type jQuery
44          * @param String url The URL of the HTML file to load.
45          * @param Hash params A set of key/value pairs that will be sent to the server.
46          * @param Function callback A function to be executed whenever the data is loaded.
47          * @cat AJAX
48          */
49         load: function( url, params, callback, ifModified ) {
50                 if ( url.constructor == Function )
51                         return this.bind("load", url);
52         
53                 callback = callback || function(){};
54         
55                 // Default to a GET request
56                 var type = "GET";
57         
58                 // If the second parameter was provided
59                 if ( params ) {
60                         // If it's a function
61                         if ( params.constructor == Function ) {
62                                 // We assume that it's the callback
63                                 callback = params;
64                                 params = null;
65                                 
66                         // Otherwise, build a param string
67                         } else {
68                                 params = jQuery.param( params );
69                                 type = "POST";
70                         }
71                 }
72                 
73                 var self = this;
74                 
75                 // Request the remote document
76                 jQuery.ajax( type, url, params,function(res, status){
77                         
78                         if ( status == "success" || !ifModified && status == "notmodified" ) {
79                                 // Inject the HTML into all the matched elements
80                                 self.html(res.responseText).each( callback, [res.responseText, status] );
81                                 
82                                 // Execute all the scripts inside of the newly-injected HTML
83                                 $("script", self).each(function(){
84                                         if ( this.src )
85                                                 $.getScript( this.src );
86                                         else
87                                                 eval.call( window, this.text || this.textContent || this.innerHTML || "" );
88                                 });
89                         } else
90                                 callback.apply( self, [res.responseText, status] );
91         
92                 }, ifModified);
93                 
94                 return this;
95         },
96
97         /**
98          * A function for serializing a set of input elements into
99          * a string of data.
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&location=Boston
105          * @desc Serialize a selection of input elements to a string
106          *
107          * @test var data = $(':input').serialize();
108          * ok( data == 'action=Test&text2=Test&radio1=on&radio2=on&check=on&=on&hidden=&foo[bar]=&name=name&button=&=foobar&select1=&select2=3&select3=1', 'Check form serialization as query string' );
109          *
110          * @name serialize
111          * @type String
112          * @cat AJAX
113          */
114         serialize: function() {
115                 return $.param( this );
116         }
117         
118 });
119
120 // If IE is used, create a wrapper for the XMLHttpRequest object
121 if ( jQuery.browser.msie && typeof XMLHttpRequest == "undefined" )
122         XMLHttpRequest = function(){
123                 return new ActiveXObject(
124                         navigator.userAgent.indexOf("MSIE 5") >= 0 ?
125                         "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP"
126                 );
127         };
128
129 // Attach a bunch of functions for handling common AJAX events
130
131 /**
132  * Attach a function to be executed whenever an AJAX request begins.
133  *
134  * @example $("#loading").ajaxStart(function(){
135  *   $(this).show();
136  * });
137  * @desc Show a loading message whenever an AJAX request starts.
138  *
139  * @name ajaxStart
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 all AJAX requests have ended.
147  *
148  * @example $("#loading").ajaxStop(function(){
149  *   $(this).hide();
150  * });
151  * @desc Hide a loading message after all the AJAX requests have stopped.
152  *
153  * @name ajaxStop
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 an AJAX request completes.
161  *
162  * @example $("#msg").ajaxComplete(function(){
163  *   $(this).append("<li>Request Complete.</li>");
164  * });
165  * @desc Show a message when an AJAX request completes.
166  *
167  * @name ajaxComplete
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  * successfully.
176  *
177  * @example $("#msg").ajaxSuccess(function(){
178  *   $(this).append("<li>Successful Request!</li>");
179  * });
180  * @desc Show a message when an AJAX request completes successfully.
181  *
182  * @name ajaxSuccess
183  * @type jQuery
184  * @param Function callback The function to execute.
185  * @cat AJAX
186  */
187  
188 /**
189  * Attach a function to be executed whenever an AJAX request fails.
190  *
191  * @example $("#msg").ajaxError(function(){
192  *   $(this).append("<li>Error requesting page.</li>");
193  * });
194  * @desc Show a message when an AJAX request fails.
195  *
196  * @name ajaxError
197  * @type jQuery
198  * @param Function callback The function to execute.
199  * @cat AJAX
200  */
201  
202 /**
203  * @test stop(); var counter = { complete: 0, success: 0, error: 0 };
204  * var success = function() { counter.success++ };
205  * var error = function() { counter.error++ };
206  * var complete = function() { counter.complete++ };
207  * $('#foo').ajaxStart(complete).ajaxStop(complete).ajaxComplete(complete).ajaxError(error).ajaxSuccess(success);
208  * // start with successful test
209  * $.ajax({url: "data/name.php", success: success, error: error, complete: function() {
210  *   ok( counter.error == 0, 'Check succesful request' );
211  *   ok( counter.success == 2, 'Check succesful request' );
212  *   ok( counter.complete == 3, 'Check succesful request' );
213  *   counter.error = 0; counter.success = 0; counter.complete = 0;
214  *   $.ajaxTimeout(500);
215  *   $.ajax({url: "data/name.php?wait=5", success: success, error: error, complete: function() {
216  *     ok( counter.error == 2, 'Check failed request' );
217  *     ok( counter.success == 0, 'Check failed request' );
218  *     ok( counter.complete == 3, 'Check failed request' );
219  *     counter.error = 0; counter.success = 0; counter.complete = 0;
220  *     $.ajaxTimeout(0);
221  *     $.ajax({url: "data/name.php?wait=5", global: false, success: success, error: error, complete: function() {
222  *       ok( counter.error == 0, 'Check sucesful request without globals' );
223  *       ok( counter.success == 1, 'Check sucesful request without globals' );
224  *       ok( counter.complete == 0, 'Check sucesful request without globals' );
225  *       counter.error = 0; counter.success = 0; counter.complete = 0;
226  *       $.ajaxTimeout(500);
227  *       $.ajax({url: "data/name.php?wait=5", global: false, success: success, error: error, complete: function() {
228  *         ok( counter.error == 1, 'Check failedrequest without globals' );
229  *         ok( counter.success == 0, 'Check failed request without globals' );
230  *         ok( counter.complete == 0, 'Check failed request without globals' );
231  *         start();
232  *       }});
233  *     }});
234  *   }});
235  * }});
236  * 
237  * @name ajaxHandlersTesting
238  * @private
239  */
240  
241
242 new function(){
243         var e = "ajaxStart,ajaxStop,ajaxComplete,ajaxError,ajaxSuccess".split(",");
244         
245         for ( var i = 0; i < e.length; i++ ) new function(){
246                 var o = e[i];
247                 jQuery.fn[o] = function(f){
248                         return this.bind(o, f);
249                 };
250         };
251 };
252
253 jQuery.extend({
254
255         /**
256          * Load a remote page using an HTTP GET request. All of the arguments to
257          * the method (except URL) are optional.
258          *
259          * @example $.get("test.cgi")
260          *
261          * @example $.get("test.cgi", { name: "John", time: "2pm" } )
262          *
263          * @example $.get("test.cgi", function(data){
264          *   alert("Data Loaded: " + data);
265          * })
266          *
267          * @example $.get("test.cgi",
268          *   { name: "John", time: "2pm" },
269          *   function(data){
270          *     alert("Data Loaded: " + data);
271          *   }
272          * )
273          *
274          * @test stop();
275          * $.get("data/dashboard.xml", function(xml) {
276          *     var content = [];
277      *     $('tab', xml).each(function(k) {
278      *         // workaround for IE needed here, $(this).text() throws an error
279      *         // content[k] = $.trim(this.firstChild.data) || $(this).text();
280      *         content[k] = $(this).text();
281      *     });
282          *         ok( content[0] && content[0].match(/blabla/), 'Check first tab' );
283          *     ok( content[1] && content[1].match(/blublu/), 'Check second tab' );
284          *     start();
285          * });
286          *
287          * @name $.get
288          * @type jQuery
289          * @param String url The URL of the page to load.
290          * @param Hash params A set of key/value pairs that will be sent to the server.
291          * @param Function callback A function to be executed whenever the data is loaded.
292          * @cat AJAX
293          */
294         get: function( url, data, callback, type, ifModified ) {
295                 if ( data.constructor == Function ) {
296                         type = callback;
297                         callback = data;
298                         data = null;
299                 }
300                 
301                 if ( data ) url += "?" + jQuery.param(data);
302                 
303                 // Build and start the HTTP Request
304                 jQuery.ajax( "GET", url, null, function(r, status) {
305                         if ( callback ) callback( jQuery.httpData(r,type), status );
306                 }, ifModified);
307         },
308         
309         /**
310          * Load a remote page using an HTTP GET request, only if it hasn't
311          * been modified since it was last retrieved. All of the arguments to
312          * the method (except URL) are optional.
313          *
314          * @example $.getIfModified("test.html")
315          *
316          * @example $.getIfModified("test.html", { name: "John", time: "2pm" } )
317          *
318          * @example $.getIfModified("test.cgi", function(data){
319          *   alert("Data Loaded: " + data);
320          * })
321          *
322          * @example $.getifModified("test.cgi",
323          *   { name: "John", time: "2pm" },
324          *   function(data){
325          *     alert("Data Loaded: " + data);
326          *   }
327          * )
328          *
329          * @test stop();
330          * $.getIfModified("data/name.php", function(msg) {
331          *     ok( msg == 'ERROR', 'Check ifModified' );
332          *     start();
333          * });
334          *
335          * @name $.getIfModified
336          * @type jQuery
337          * @param String url The URL of the page to load.
338          * @param Hash params A set of key/value pairs that will be sent to the server.
339          * @param Function callback A function to be executed whenever the data is loaded.
340          * @cat AJAX
341          */
342         getIfModified: function( url, data, callback, type ) {
343                 jQuery.get(url, data, callback, type, 1);
344         },
345
346         /**
347          * Loads, and executes, a remote JavaScript file using an HTTP GET request.
348          * All of the arguments to the method (except URL) are optional.
349          *
350          * @example $.getScript("test.js")
351          *
352          * @example $.getScript("test.js", function(){
353          *   alert("Script loaded and executed.");
354          * })
355          *
356          * @test stop();
357          * $.getScript("data/test.js", function() {
358          *      ok( foobar == "bar", 'Check if script was evaluated' );
359          *      start();
360          * });
361          *
362          * @name $.getScript
363          * @type jQuery
364          * @param String url The URL of the page to load.
365          * @param Function callback A function to be executed whenever the data is loaded.
366          * @cat AJAX
367          */
368         getScript: function( url, callback ) {
369                 jQuery.get(url, callback, "script");
370         },
371         
372         /**
373          * Load a remote JSON object using an HTTP GET request.
374          * All of the arguments to the method (except URL) are optional.
375          *
376          * @example $.getJSON("test.js", function(json){
377          *   alert("JSON Data: " + json.users[3].name);
378          * })
379          *
380          * @example $.getJSON("test.js",
381          *   { name: "John", time: "2pm" },
382          *   function(json){
383          *     alert("JSON Data: " + json.users[3].name);
384          *   }
385          * )
386          *
387          * @test stop();
388          * $.getJSON("data/json.php", {json: "array"}, function(json) {
389          *   ok( json[0].name == 'John', 'Check JSON: first, name' );
390          *   ok( json[0].age == 21, 'Check JSON: first, age' );
391          *   ok( json[1].name == 'Peter', 'Check JSON: second, name' );
392          *   ok( json[1].age == 25, 'Check JSON: second, age' );
393          *   start();
394          * });
395          * @test stop();
396          * $.getJSON("data/json.php", function(json) {
397          *   ok( json.data.lang == 'en', 'Check JSON: lang' );
398          *   ok( json.data.length == 25, 'Check JSON: length' );
399          *   start();
400          * });
401          *
402          * @name $.getJSON
403          * @type jQuery
404          * @param String url The URL of the page to load.
405          * @param Hash params A set of key/value pairs that will be sent to the server.
406          * @param Function callback A function to be executed whenever the data is loaded.
407          * @cat AJAX
408          */
409         getJSON: function( url, data, callback ) {
410                 if(callback)
411                         jQuery.get(url, data, callback, "json");
412                 else {
413                         jQuery.get(url, data, "json");
414                 }
415         },
416         
417         /**
418          * Load a remote page using an HTTP POST request. All of the arguments to
419          * the method (except URL) are optional.
420          *
421          * @example $.post("test.cgi")
422          *
423          * @example $.post("test.cgi", { name: "John", time: "2pm" } )
424          *
425          * @example $.post("test.cgi", function(data){
426          *   alert("Data Loaded: " + data);
427          * })
428          *
429          * @example $.post("test.cgi",
430          *   { name: "John", time: "2pm" },
431          *   function(data){
432          *     alert("Data Loaded: " + data);
433          *   }
434          * )
435          *
436          * @test stop();
437          * $.post("data/name.php", {xml: "5-2"}, function(xml){
438          *   $('math', xml).each(function() {
439          *          ok( $('calculation', this).text() == '5-2', 'Check for XML' );
440          *          ok( $('result', this).text() == '3', 'Check for XML' );
441          *       });
442          *   start();
443          * });
444          *
445          * @name $.post
446          * @type jQuery
447          * @param String url The URL of the page to load.
448          * @param Hash params A set of key/value pairs that will be sent to the server.
449          * @param Function callback A function to be executed whenever the data is loaded.
450          * @cat AJAX
451          */
452         post: function( url, data, callback, type ) {
453                 // Build and start the HTTP Request
454                 jQuery.ajax( "POST", url, jQuery.param(data), function(r, status) {
455                         if ( callback ) callback( jQuery.httpData(r,type), status );
456                 });
457         },
458         
459         // timeout (ms)
460         timeout: 0,
461
462         /**
463          * Set the timeout of all AJAX requests to a specific amount of time.
464          * This will make all future AJAX requests timeout after a specified amount
465          * of time (the default is no timeout).
466          *
467          * @example $.ajaxTimeout( 5000 );
468          * @desc Make all AJAX requests timeout after 5 seconds.
469          *
470          * @test stop();
471          * var passed = 0;
472          * var timeout;
473          * $.ajaxTimeout(1000);
474          * var pass = function() {
475          *      passed++;
476          *      if(passed == 2) {
477          *              ok( true, 'Check local and global callbacks after timeout' );
478          *              clearTimeout(timeout);
479          *      $('#main').unbind("ajaxError");
480          *              start();
481          *      }
482          * };
483          * var fail = function() {
484          *      ok( false, 'Check for timeout failed' );
485          *      start();
486          * };
487          * timeout = setTimeout(fail, 1500);
488          * $('#main').ajaxError(pass);
489          * $.ajax({
490          *   type: "GET",
491          *   url: "data/name.php?wait=5",
492          *   error: pass,
493          *   success: fail
494          * });
495          *
496          * @test stop(); $.ajaxTimeout(50);
497          * $.ajax({
498          *   type: "GET",
499          *   timeout: 5000,
500          *   url: "data/name.php?wait=1",
501          *   error: function() {
502          *         ok( false, 'Check for local timeout failed' );
503          *         start();
504          *   },
505          *   success: function() {
506          *     ok( true, 'Check for local timeout' );
507          *     start();
508          *   }
509          * });
510          * 
511          *
512          * @name $.ajaxTimeout
513          * @type jQuery
514          * @param Number time How long before an AJAX request times out.
515          * @cat AJAX
516          */
517         ajaxTimeout: function(timeout) {
518                 jQuery.timeout = timeout;
519         },
520
521         // Last-Modified header cache for next request
522         lastModified: {},
523         
524         /**
525          * Load a remote page using an HTTP request. This function is the primary
526          * means of making AJAX requests using jQuery. $.ajax() takes one property,
527          * an object of key/value pairs, that're are used to initalize the request.
528          *
529          * These are all the key/values that can be passed in to 'prop':
530          *
531          * (String) type - The type of request to make (e.g. "POST" or "GET").
532          *
533          * (String) url - The URL of the page to request.
534          * 
535          * (String) data - A string of data to be sent to the server (POST only).
536          *
537          * (String) dataType - The type of data that you're expecting back from
538          * the server (e.g. "xml", "html", "script", or "json").
539          *
540          * (Boolean) ifModified - Allow the request to be successful only if the
541          * response has changed since the last request, default is false, ignoring
542          * the Last-Modified header
543          *
544          * (Number) timeout - Local timeout to override global timeout, eg. to give a
545          * single request a longer timeout while all others timeout after 1 seconds,
546          * see $.ajaxTimeout
547          *
548          * (Boolean) global - Wheather to trigger global AJAX event handlers for
549          * this request, default is true. Set to true to prevent that global handlers
550          * like ajaxStart or ajaxStop are triggered.
551          *
552          * (Function) error - A function to be called if the request fails. The
553          * function gets passed two arguments: The XMLHttpRequest object and a
554          * string describing the type of error that occurred.
555          *
556          * (Function) success - A function to be called if the request succeeds. The
557          * function gets passed one argument: The data returned from the server,
558          * formatted according to the 'dataType' parameter.
559          *
560          * (Function) complete - A function to be called when the request finishes. The
561          * function gets passed two arguments: The XMLHttpRequest object and a
562          * string describing the type the success of the request.
563          *
564          * @example $.ajax({
565          *   type: "GET",
566          *   url: "test.js",
567          *   dataType: "script"
568          * })
569          * @desc Load and execute a JavaScript file.
570          *
571          * @example $.ajax({
572          *   type: "POST",
573          *   url: "some.php",
574          *   data: "name=John&location=Boston",
575          *   success: function(msg){
576          *     alert( "Data Saved: " + msg );
577          *   }
578          * });
579          * @desc Save some data to the server and notify the user once its complete.
580          *
581          * @test stop();
582          * $.ajax({
583          *   type: "GET",
584          *   url: "data/name.php?name=foo",
585          *   success: function(msg){
586          *     ok( msg == 'bar', 'Check for GET' );
587          *     start();
588          *   }
589          * });
590          *
591          * @test stop();
592          * $.ajax({
593          *   type: "POST",
594          *   url: "data/name.php",
595          *   data: "name=peter",
596          *   success: function(msg){
597          *     ok( msg == 'pan', 'Check for POST' );
598          *     start();
599          *   }
600          * });
601          *
602          * @name $.ajax
603          * @type jQuery
604          * @param Hash prop A set of properties to initialize the request with.
605          * @cat AJAX
606          */
607         ajax: function( type, url, data, ret, ifModified ) {
608                 // If only a single argument was passed in,
609                 // assume that it is a object of key/value pairs
610                 if ( !url ) {
611                         ret = type.complete;
612                         var success = type.success;
613                         var error = type.error;
614                         var dataType = type.dataType;
615                         var global = typeof type.global == "boolean" ? type.global : true;
616                         var timeout = typeof type.timeout == "number" ? type.timeout : jQuery.timeout;
617                         var ifModified = type.ifModified || false;
618                         data = type.data;
619                         url = type.url;
620                         type = type.type;
621                 }
622                 
623                 // Watch for a new set of requests
624                 if ( global && ! jQuery.active++ )
625                         jQuery.event.trigger( "ajaxStart" );
626
627                 var requestDone = false;
628         
629                 // Create the request object
630                 var xml = new XMLHttpRequest();
631         
632                 // Open the socket
633                 xml.open(type || "GET", url, true);
634                 
635                 // Set the correct header, if data is being sent
636                 if ( data )
637                         xml.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
638                 
639                 // Set the If-Modified-Since header, if ifModified mode.
640                 if ( ifModified )
641                         xml.setRequestHeader("If-Modified-Since",
642                                 jQuery.lastModified[url] || "Thu, 01 Jan 1970 00:00:00 GMT" );
643                 
644                 // Set header so the called script knows that it's an XMLHttpRequest
645                 xml.setRequestHeader("X-Requested-With", "XMLHttpRequest");
646         
647                 // Make sure the browser sends the right content length
648                 if ( xml.overrideMimeType )
649                         xml.setRequestHeader("Connection", "close");
650                 
651                 // Wait for a response to come back
652                 var onreadystatechange = function(istimeout){
653                         // The transfer is complete and the data is available, or the request timed out
654                         if ( xml && (xml.readyState == 4 || istimeout == "timeout") ) {
655                                 requestDone = true;
656
657                                 var status = jQuery.httpSuccess( xml ) && istimeout != "timeout" ?
658                                         ifModified && jQuery.httpNotModified( xml, url ) ? "notmodified" : "success" : "error";
659                                 
660                                 // Make sure that the request was successful or notmodified
661                                 if ( status != "error" ) {
662                                         // Cache Last-Modified header, if ifModified mode.
663                                         var modRes;
664                                         try {
665                                                 modRes = xml.getResponseHeader("Last-Modified");
666                                         } catch(e) {} // swallow exception thrown by FF if header is not available
667                                         
668                                         if ( ifModified && modRes )
669                                                 jQuery.lastModified[url] = modRes;
670                                         
671                                         // If a local callback was specified, fire it
672                                         if ( success )
673                                                 success( jQuery.httpData( xml, dataType ), status );
674                                         
675                                         // Fire the global callback
676                                         if( global )
677                                                 jQuery.event.trigger( "ajaxSuccess" );
678                                 
679                                 // Otherwise, the request was not successful
680                                 } else {
681                                         // If a local callback was specified, fire it
682                                         if ( error ) error( xml, status );
683                                         
684                                         // Fire the global callback
685                                         if( global )
686                                                 jQuery.event.trigger( "ajaxError" );
687                                 }
688                                 
689                                 // The request was completed
690                                 if( global )
691                                         jQuery.event.trigger( "ajaxComplete" );
692                                 
693                                 // Handle the global AJAX counter
694                                 if ( global && ! --jQuery.active )
695                                         jQuery.event.trigger( "ajaxStop" );
696         
697                                 // Process result
698                                 if ( ret ) ret(xml, status);
699                                 
700                                 // Stop memory leaks
701                                 xml.onreadystatechange = function(){};
702                                 xml = null;
703                                 
704                         }
705                 };
706                 xml.onreadystatechange = onreadystatechange;
707                 
708                 // Timeout checker
709                 if(timeout > 0)
710                         setTimeout(function(){
711                                 // Check to see if the request is still happening
712                                 if (xml) {
713                                         // Cancel the request
714                                         xml.abort();
715
716                                         if ( !requestDone ) onreadystatechange( "timeout" );
717
718                                         // Clear from memory
719                                         xml = null;
720                                 }
721                         }, timeout);
722                 
723                 // Send the data
724                 xml.send(data);
725         },
726         
727         // Counter for holding the number of active queries
728         active: 0,
729         
730         // Determines if an XMLHttpRequest was successful or not
731         httpSuccess: function(r) {
732                 try {
733                         return !r.status && location.protocol == "file:" ||
734                                 ( r.status >= 200 && r.status < 300 ) || r.status == 304 ||
735                                 jQuery.browser.safari && r.status == undefined;
736                 } catch(e){}
737
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
751                 return false;
752         },
753         
754         /* Get the data out of an XMLHttpRequest.
755          * Return parsed XML if content-type header is "xml" and type is "xml" or omitted,
756          * otherwise return plain text.
757          * (String) data - The type of data that you're expecting back,
758          * (e.g. "xml", "html", "script")
759          */
760         httpData: function(r,type) {
761                 var ct = r.getResponseHeader("content-type");
762                 var data = !type && ct && ct.indexOf("xml") >= 0;
763                 data = type == "xml" || data ? r.responseXML : r.responseText;
764
765                 // If the type is "script", eval it
766                 if ( type == "script" ) eval.call( window, data );
767
768                 // Get the JavaScript object, if JSON is used.
769                 if ( type == "json" ) eval( "data = " + data );
770
771                 return data;
772         },
773         
774         // Serialize an array of form elements or a set of
775         // key/values into a query string
776         param: function(a) {
777                 var s = [];
778                 
779                 // If an array was passed in, assume that it is an array
780                 // of form elements
781                 if ( a.constructor == Array || a.jquery ) {
782                         // Serialize the form elements
783                         for ( var i = 0; i < a.length; i++ )
784                                 s.push( a[i].name + "=" + encodeURIComponent( a[i].value ) );
785                         
786                 // Otherwise, assume that it's an object of key/value pairs
787                 } else {
788                         // Serialize the key/values
789                         for ( var j in a )
790                                 s.push( j + "=" + encodeURIComponent( a[j] ) );
791                 }
792                 
793                 // Return the resulting serialization
794                 return s.join("&");
795         }
796
797 });