Switched to using new Function instead of eval for handling JSON parsing (Fixes bug...
[jquery.git] / src / ajax.js
index d02d331..4db08a4 100644 (file)
@@ -71,7 +71,7 @@ jQuery.fn.extend({
                .filter(function(){
                        return this.name && !this.disabled &&
                                (this.checked || /select|textarea/i.test(this.nodeName) ||
-                                       /text|hidden|password/i.test(this.type));
+                                       /text|hidden|password|search/i.test(this.type));
                })
                .map(function(i, elem){
                        var val = jQuery(this).val();
@@ -95,7 +95,7 @@ jQuery.each( "ajaxStart,ajaxStop,ajaxComplete,ajaxError,ajaxSuccess,ajaxSend".sp
 var jsc = now();
 
 jQuery.extend({
-  
+
        get: function( url, data, callback, type ) {
                // shift arguments if data argument was ommited
                if ( jQuery.isFunction( data ) ) {
@@ -143,13 +143,15 @@ jQuery.extend({
                url: location.href,
                global: true,
                type: "GET",
-               timeout: 0,
                contentType: "application/x-www-form-urlencoded",
                processData: true,
                async: true,
+               /*
+               timeout: 0,
                data: null,
                username: null,
                password: null,
+               */
                // Create the request object; Microsoft failed to properly
                // implement the XMLHttpRequest in IE7, so we use the ActiveXObject when it is available
                // This function can be overriden by calling jQuery.ajaxSetup
@@ -231,9 +233,6 @@ jQuery.extend({
                // If data is available, append data to url for get requests
                if ( s.data && type == "GET" ) {
                        s.url += (s.url.match(/\?/) ? "&" : "?") + s.data;
-
-                       // IE likes to send both get and post data, prevent this
-                       s.data = null;
                }
 
                // Watch for a new set of requests
@@ -265,12 +264,17 @@ jQuery.extend({
                                                done = true;
                                                success();
                                                complete();
+
+                                               // Handle memory leak in IE
+                                               script.onload = script.onreadystatechange = null;
                                                head.removeChild( script );
                                        }
                                };
                        }
 
-                       head.appendChild(script);
+                       // Use insertBefore instead of appendChild  to circumvent an IE6 bug.
+                       // This arises when a base node is used (#2709 and #4378).
+                       head.insertBefore( script, head.firstChild );
 
                        // We handle everything using the script element injection
                        return undefined;
@@ -378,6 +382,9 @@ jQuery.extend({
                                // Fire the complete handlers
                                complete();
 
+                               if ( isTimeout )
+                                       xhr.abort();
+
                                // Stop memory leaks
                                if ( s.async )
                                        xhr = null;
@@ -392,20 +399,14 @@ jQuery.extend({
                        if ( s.timeout > 0 )
                                setTimeout(function(){
                                        // Check to see if the request is still happening
-                                       if ( xhr ) {
-                                               if( !requestDone )
-                                                       onreadystatechange( "timeout" );
-
-                                               // Cancel the request
-                                               if ( xhr )
-                                                       xhr.abort();
-                                       }
+                                       if ( xhr && !requestDone )
+                                               onreadystatechange( "timeout" );
                                }, s.timeout);
                }
 
                // Send the data
                try {
-                       xhr.send(s.data);
+                       xhr.send( type === "POST" ? s.data : null );
                } catch(e) {
                        jQuery.handleError(s, xhr, null, e);
                }
@@ -459,8 +460,7 @@ jQuery.extend({
                try {
                        // IE error sometimes returns 1223 when it should be 204 so treat it as success, see #1450
                        return !xhr.status && location.protocol == "file:" ||
-                               ( xhr.status >= 200 && xhr.status < 300 ) || xhr.status == 304 || xhr.status == 1223 ||
-                               jQuery.browser.safari && xhr.status === undefined;
+                               ( xhr.status >= 200 && xhr.status < 300 ) || xhr.status == 304 || xhr.status == 1223;
                } catch(e){}
                return false;
        },
@@ -471,8 +471,7 @@ jQuery.extend({
                        var xhrRes = xhr.getResponseHeader("Last-Modified");
 
                        // Firefox always returns 200. check Last-Modified date
-                       return xhr.status == 304 || xhrRes == jQuery.lastModified[url] ||
-                               jQuery.browser.safari && xhr.status === undefined;
+                       return xhr.status == 304 || xhrRes == jQuery.lastModified[url];
                } catch(e){}
                return false;
        },
@@ -482,26 +481,34 @@ jQuery.extend({
                        xml = type == "xml" || !type && ct && ct.indexOf("xml") >= 0,
                        data = xml ? xhr.responseXML : xhr.responseText;
 
-               if ( xml && data.documentElement.tagName == "parsererror" )
+               if ( xml && data.documentElement.tagName == "parsererror" ) {
                        throw "parsererror";
-                       
+               }
+
                // Allow a pre-filtering function to sanitize the response
                // s != null is checked to keep backwards compatibility
-               if( s && s.dataFilter )
+               if ( s && s.dataFilter ) {
                        data = s.dataFilter( data, type );
+               }
 
                // The filter can actually parse the response
-               if( typeof data === "string" ){
+               if ( typeof data === "string" ) {
 
                        // If the type is "script", eval it in global context
-                       if ( type == "script" )
+                       if ( type === "script" ) {
                                jQuery.globalEval( data );
+                       }
 
                        // Get the JavaScript object, if JSON is used.
-                       if ( type == "json" )
-                               data = eval("(" + data + ")");
+                       if ( type == "json" ) {
+                               if ( typeof JSON === "object" && JSON.parse ) {
+                                       data = JSON.parse( data );
+                               } else {
+                                       data = (new Function("return " + data))();
+                               }
+                       }
                }
-               
+
                return data;
        },