X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fajax.js;h=beed45fbfdf596b269924836a3c0ba75ea74f5da;hb=991d039b62f5dfcb9e3d99fe28212a6874e8f5c7;hp=7e024a319732d41f18cfa79b27d29ac10f67a2c7;hpb=325755d4b38e87b13f9c24a4c2991ec497552aad;p=jquery.git diff --git a/src/ajax.js b/src/ajax.js index 7e024a3..beed45f 100644 --- a/src/ajax.js +++ b/src/ajax.js @@ -3,7 +3,7 @@ jQuery.fn.extend({ _load: jQuery.fn.load, load: function( url, params, callback ) { - if ( typeof url != 'string' ) + if ( typeof url !== "string" ) return this._load( url ); var off = url.indexOf(" "); @@ -24,7 +24,7 @@ jQuery.fn.extend({ params = null; // Otherwise, build a param string - } else if( typeof params == 'object' ) { + } else if( typeof params === "object" ) { params = jQuery.param( params ); type = "POST"; } @@ -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 @@ -168,6 +170,7 @@ jQuery.extend({ // Last-Modified header cache for next request lastModified: {}, + etag: {}, ajax: function( s ) { // Extend the settings, but re-extend 's' so that it can be @@ -178,7 +181,7 @@ jQuery.extend({ type = s.type.toUpperCase(); // convert data if not already a string - if ( s.data && s.processData && typeof s.data != "string" ) + if ( s.data && s.processData && typeof s.data !== "string" ) s.data = jQuery.param(s.data); // Handle JSONP Parameter Callbacks @@ -231,9 +234,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 +265,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; @@ -294,10 +299,13 @@ jQuery.extend({ if ( s.data ) xhr.setRequestHeader("Content-Type", s.contentType); - // Set the If-Modified-Since header, if ifModified mode. - if ( s.ifModified ) - xhr.setRequestHeader("If-Modified-Since", - jQuery.lastModified[s.url] || "Thu, 01 Jan 1970 00:00:00 GMT" ); + // Set the If-Modified-Since and/or If-None-Match header, if in ifModified mode. + if ( s.ifModified ) { + if (jQuery.lastModified[s.url]) + xhr.setRequestHeader("If-Modified-Since", jQuery.lastModified[s.url]); + if (jQuery.etag[s.url]) + xhr.setRequestHeader("If-None-Match", jQuery.etag[s.url]); + } // Set header so the called script knows that it's an XMLHttpRequest xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest"); @@ -308,10 +316,11 @@ jQuery.extend({ s.accepts._default ); } catch(e){} - // Allow custom headers/mimetypes + // Allow custom headers/mimetypes and early abort if ( s.beforeSend && s.beforeSend(xhr, s) === false ) { - // cleanup active request counter - s.global && jQuery.active--; + // Handle the global AJAX counter + if ( s.global && ! --jQuery.active ) + jQuery.event.trigger( "ajaxStop" ); // close opended socket xhr.abort(); return false; @@ -322,8 +331,18 @@ jQuery.extend({ // Wait for a response to come back var onreadystatechange = function(isTimeout){ + // The request was aborted, clear the interval and decrement jQuery.active + if (xhr.readyState == 0) { + if (ival) { + // clear poll interval + clearInterval(ival); + ival = null; + // Handle the global AJAX counter + if ( s.global && ! --jQuery.active ) + jQuery.event.trigger( "ajaxStop" ); + } // The transfer is complete and the data is available, or the request timed out - if ( !requestDone && xhr && (xhr.readyState == 4 || isTimeout == "timeout") ) { + } else if ( !requestDone && xhr && (xhr.readyState == 4 || isTimeout == "timeout") ) { requestDone = true; // clear poll interval @@ -348,16 +367,7 @@ jQuery.extend({ } // Make sure that the request was successful or notmodified - if ( status == "success" ) { - // Cache Last-Modified header, if ifModified mode. - var modRes; - try { - modRes = xhr.getResponseHeader("Last-Modified"); - } catch(e) {} // swallow exception thrown by FF if header is not available - - if ( s.ifModified && modRes ) - jQuery.lastModified[s.url] = modRes; - + if ( status == "success" || status == "notmodified" ) { // JSONP handles its own success callback if ( !jsonp ) success(); @@ -367,6 +377,9 @@ jQuery.extend({ // Fire the complete handlers complete(); + if ( isTimeout ) + xhr.abort(); + // Stop memory leaks if ( s.async ) xhr = null; @@ -381,19 +394,14 @@ jQuery.extend({ if ( s.timeout > 0 ) setTimeout(function(){ // Check to see if the request is still happening - if ( xhr ) { - // Cancel the request - xhr.abort(); - - if( !requestDone ) - onreadystatechange( "timeout" ); - } + 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); } @@ -447,22 +455,25 @@ 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; + // Opera returns 0 when status is 304 + ( xhr.status >= 200 && xhr.status < 300 ) || xhr.status == 304 || xhr.status == 1223 || xhr.status == 0; } catch(e){} return false; }, // Determines if an XMLHttpRequest returns NotModified httpNotModified: function( xhr, url ) { - try { - var xhrRes = xhr.getResponseHeader("Last-Modified"); + var last_modified = xhr.getResponseHeader("Last-Modified"); + var etag = xhr.getResponseHeader("Etag"); - // Firefox always returns 200. check Last-Modified date - return xhr.status == 304 || xhrRes == jQuery.lastModified[url] || - jQuery.browser.safari && xhr.status == undefined; - } catch(e){} - return false; + if (last_modified) + jQuery.lastModified[url] = last_modified; + + if (etag) + jQuery.etag[url] = etag; + + // Opera returns 0 when status is 304 + return xhr.status == 304 || xhr.status == 0; }, httpData: function( xhr, type, s ) { @@ -470,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; },