Fix for #991
[jquery.git] / src / ajax / ajax.js
index f87f562..814a6b8 100644 (file)
@@ -44,7 +44,7 @@ jQuery.fn.extend({
         * @cat Ajax
         */
        load: function( url, params, callback, ifModified ) {
-               if ( url.constructor == Function )
+               if ( jQuery.isFunction( url ) )
                        return this.bind("load", url);
 
                callback = callback || function(){};
@@ -55,7 +55,7 @@ jQuery.fn.extend({
                // If the second parameter was provided
                if ( params )
                        // If it's a function
-                       if ( params.constructor == Function ) {
+                       if ( jQuery.isFunction( params ) ) {
                                // We assume that it's the callback
                                callback = params;
                                params = null;
@@ -77,7 +77,7 @@ jQuery.fn.extend({
                        complete: function(res, status){
                                if ( status == "success" || !ifModified && status == "notmodified" )
                                        // Inject the HTML into all the matched elements
-                                       self.html(res.responseText)
+                                       self.attr("innerHTML", res.responseText)
                                          // Execute all the scripts inside of the newly-injected HTML
                                          .evalScripts()
                                          // Execute callback
@@ -121,7 +121,7 @@ jQuery.fn.extend({
         * @cat Ajax
         */
        evalScripts: function() {
-               return this.find('script').each(function(){
+               return this.find("script").each(function(){
                        if ( this.src )
                                jQuery.getScript( this.src );
                        else
@@ -132,7 +132,7 @@ jQuery.fn.extend({
 });
 
 // If IE is used, create a wrapper for the XMLHttpRequest object
-if ( jQuery.browser.msie && typeof XMLHttpRequest == "undefined" )
+if ( !window.XMLHttpRequest )
        XMLHttpRequest = function(){
                return new ActiveXObject("Microsoft.XMLHTTP");
        };
@@ -223,7 +223,7 @@ if ( jQuery.browser.msie && typeof XMLHttpRequest == "undefined" )
  */
  
 /**
- * Attach a function to be executed before an AJAX request is send.
+ * Attach a function to be executed before an AJAX request is sent.
  *
  * The XMLHttpRequest and settings used for that request are passed
  * as arguments to the callback.
@@ -231,7 +231,7 @@ if ( jQuery.browser.msie && typeof XMLHttpRequest == "undefined" )
  * @example $("#msg").ajaxSend(function(request, settings){
  *   $(this).append("<li>Starting request at " + settings.url + "</li>");
  * });
- * @desc Show a message before an AJAX request is send.
+ * @desc Show a message before an AJAX request is sent.
  *
  * @name ajaxSend
  * @type jQuery
@@ -273,7 +273,7 @@ jQuery.extend({
         */
        get: function( url, data, callback, type, ifModified ) {
                // shift arguments if data argument was ommited
-               if ( data && data.constructor == Function ) {
+               if ( jQuery.isFunction( data ) ) {
                        callback = data;
                        data = null;
                }
@@ -391,6 +391,11 @@ jQuery.extend({
         * @cat Ajax
         */
        post: function( url, data, callback, type ) {
+               if ( jQuery.isFunction( data ) ) {
+                       callback = data;
+                       data = {};
+               }
+
                return jQuery.ajax({
                        type: "POST",
                        url: url,
@@ -457,7 +462,8 @@ jQuery.extend({
                timeout: 0,
                contentType: "application/x-www-form-urlencoded",
                processData: true,
-               async: true
+               async: true,
+               data: null
        },
        
        // Last-Modified header cache for next request
@@ -536,7 +542,7 @@ jQuery.extend({
         * the default content-type "application/x-www-form-urlencoded". If you want to send
         * DOMDocuments, set this option to false.
         *
-        * (Boolean) async - By default, all requests are send asynchronous (set to true).
+        * (Boolean) async - By default, all requests are sent asynchronous (set to true).
         * If you need synchronous requests, set this option to false.
         *
         * (Function) beforeSend - A pre-callback to set custom headers etc., the
@@ -590,12 +596,15 @@ jQuery.extend({
                // if data available
                if ( s.data ) {
                        // convert data if not already a string
-                       if (s.processData && typeof s.data != 'string')
+                       if (s.processData && typeof s.data != "string")
                        s.data = jQuery.param(s.data);
                        // append data to url for get requests
-                       if( s.type.toLowerCase() == "get" )
+                       if( s.type.toLowerCase() == "get" ) {
                                // "?" + data or "&" + data (in case there are already params)
                                s.url += ((s.url.indexOf("?") > -1) ? "&" : "?") + s.data;
+                               // IE likes to send both get and post data, prevent this
+                               s.data = null;
+                       }
                }
 
                // Watch for a new set of requests
@@ -638,6 +647,13 @@ jQuery.extend({
                        // The transfer is complete and the data is available, or the request timed out
                        if ( xml && (xml.readyState == 4 || isTimeout == "timeout") ) {
                                requestDone = true;
+                               
+                               // clear poll interval
+                               if (ival) {
+                                       clearInterval(ival);
+                                       ival = null;
+                               }
+                               
                                var status;
                                try {
                                        status = jQuery.httpSuccess( xml ) && isTimeout != "timeout" ?
@@ -683,11 +699,13 @@ jQuery.extend({
                                        s.complete(xml, status);
 
                                // Stop memory leaks
-                               xml.onreadystatechange = function(){};
-                               xml = null;
+                               if(s.async)
+                                       xml = null;
                        }
                };
-               xml.onreadystatechange = onreadystatechange;
+               
+               // don't attach the handler to the request, just poll it instead
+               var ival = setInterval(onreadystatechange, 13); 
 
                // Timeout checker
                if ( s.timeout > 0 )
@@ -702,12 +720,9 @@ jQuery.extend({
                                }
                        }, s.timeout);
                        
-               // save non-leaking reference 
-               var xml2 = xml;
-
                // Send the data
                try {
-                       xml2.send(s.data);
+                       xml.send(s.data);
                } catch(e) {
                        jQuery.handleError(s, xml, null, e);
                }
@@ -717,7 +732,7 @@ jQuery.extend({
                        onreadystatechange();
                
                // return XMLHttpRequest to allow aborting the request etc.
-               return xml2;
+               return xml;
        },
 
        handleError: function( s, xml, status, e ) {
@@ -789,17 +804,19 @@ jQuery.extend({
                // of form elements
                if ( a.constructor == Array || a.jquery )
                        // Serialize the form elements
-                       for ( var i = 0; i < a.length; i++ )
-                               s.push( encodeURIComponent(a[i].name) + "=" + encodeURIComponent( a[i].value ) );
+                       jQuery.each( a, function(){
+                               s.push( encodeURIComponent(this.name) + "=" + encodeURIComponent( this.value ) );
+                       });
 
                // Otherwise, assume that it's an object of key/value pairs
                else
                        // Serialize the key/values
                        for ( var j in a )
                                // If the value is an array then the key names need to be repeated
-                               if ( a[j].constructor == Array )
-                                       for ( var k = 0; k < a[j].length; k++ )
-                                               s.push( encodeURIComponent(j) + "=" + encodeURIComponent( a[j][k] ) );
+                               if ( a[j] && a[j].constructor == Array )
+                                       jQuery.each( a[j], function(){
+                                               s.push( encodeURIComponent(j) + "=" + encodeURIComponent( this ) );
+                                       });
                                else
                                        s.push( encodeURIComponent(j) + "=" + encodeURIComponent( a[j] ) );