X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;ds=sidebyside;f=src%2Fajax.js;h=d7237fc3fb13fcfc08dac9c18bff78046cbe5ad2;hb=11761def421412df7ea4d3414310befef450b7ea;hp=36b979d342c68565431a000d15752e0feac25eac;hpb=54867a98033d7c1d2811308861ea5a92ef80ad4b;p=jquery.git diff --git a/src/ajax.js b/src/ajax.js index 36b979d..d7237fc 100644 --- a/src/ajax.js +++ b/src/ajax.js @@ -26,7 +26,7 @@ jQuery.fn.extend({ params = null; // Otherwise, build a param string - } else { + } else if( typeof params == 'object' ) { params = jQuery.param( params ); type = "POST"; } @@ -274,51 +274,51 @@ jQuery.extend({ // Create the request object; Microsoft failed to properly // implement the XMLHttpRequest in IE7, so we use the ActiveXObject when it is available - var xml = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest(); + var xhr = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest(); // Open the socket // Passing null username, generates a login popup on Opera (#2865) if( s.username ) - xml.open(type, s.url, s.async, s.username, s.password); + xhr.open(type, s.url, s.async, s.username, s.password); else - xml.open(type, s.url, s.async); + xhr.open(type, s.url, s.async); // Need an extra try/catch for cross domain requests in Firefox 3 try { // Set the correct header, if data is being sent if ( s.data ) - xml.setRequestHeader("Content-Type", s.contentType); + xhr.setRequestHeader("Content-Type", s.contentType); // Set the If-Modified-Since header, if ifModified mode. if ( s.ifModified ) - xml.setRequestHeader("If-Modified-Since", + xhr.setRequestHeader("If-Modified-Since", jQuery.lastModified[s.url] || "Thu, 01 Jan 1970 00:00:00 GMT" ); // Set header so the called script knows that it's an XMLHttpRequest - xml.setRequestHeader("X-Requested-With", "XMLHttpRequest"); + xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest"); // Set the Accepts header for the server, depending on the dataType - xml.setRequestHeader("Accept", s.dataType && s.accepts[ s.dataType ] ? + xhr.setRequestHeader("Accept", s.dataType && s.accepts[ s.dataType ] ? s.accepts[ s.dataType ] + ", */*" : s.accepts._default ); } catch(e){} // Allow custom headers/mimetypes - if ( s.beforeSend && s.beforeSend(xml, s) === false ) { + if ( s.beforeSend && s.beforeSend(xhr, s) === false ) { // cleanup active request counter s.global && jQuery.active--; // close opended socket - xml.abort(); + xhr.abort(); return false; } if ( s.global ) - jQuery.event.trigger("ajaxSend", [xml, s]); + jQuery.event.trigger("ajaxSend", [xhr, s]); // Wait for a response to come back var onreadystatechange = function(isTimeout){ // The transfer is complete and the data is available, or the request timed out - if ( !requestDone && xml && (xml.readyState == 4 || isTimeout == "timeout") ) { + if ( !requestDone && xhr && (xhr.readyState == 4 || isTimeout == "timeout") ) { requestDone = true; // clear poll interval @@ -327,16 +327,16 @@ jQuery.extend({ ival = null; } - status = isTimeout == "timeout" && "timeout" || - !jQuery.httpSuccess( xml ) && "error" || - s.ifModified && jQuery.httpNotModified( xml, s.url ) && "notmodified" || + status = isTimeout == "timeout" ? "timeout" : + !jQuery.httpSuccess( xhr ) ? "error" : + s.ifModified && jQuery.httpNotModified( xhr, s.url ) ? "notmodified" : "success"; if ( status == "success" ) { // Watch for, and catch, XML document parse errors try { // process the data (runs the xml through httpData regardless of callback) - data = jQuery.httpData( xml, s.dataType ); + data = jQuery.httpData( xhr, s.dataType, s.dataFilter ); } catch(e) { status = "parsererror"; } @@ -347,7 +347,7 @@ jQuery.extend({ // Cache Last-Modified header, if ifModified mode. var modRes; try { - modRes = xml.getResponseHeader("Last-Modified"); + modRes = xhr.getResponseHeader("Last-Modified"); } catch(e) {} // swallow exception thrown by FF if header is not available if ( s.ifModified && modRes ) @@ -357,14 +357,14 @@ jQuery.extend({ if ( !jsonp ) success(); } else - jQuery.handleError(s, xml, status); + jQuery.handleError(s, xhr, status); // Fire the complete handlers complete(); // Stop memory leaks if ( s.async ) - xml = null; + xhr = null; } }; @@ -376,9 +376,9 @@ jQuery.extend({ if ( s.timeout > 0 ) setTimeout(function(){ // Check to see if the request is still happening - if ( xml ) { + if ( xhr ) { // Cancel the request - xml.abort(); + xhr.abort(); if( !requestDone ) onreadystatechange( "timeout" ); @@ -388,9 +388,9 @@ jQuery.extend({ // Send the data try { - xml.send(s.data); + xhr.send(s.data); } catch(e) { - jQuery.handleError(s, xml, null, e); + jQuery.handleError(s, xhr, null, e); } // firefox 1.5 doesn't fire statechange for sync requests @@ -404,17 +404,17 @@ jQuery.extend({ // Fire the global callback if ( s.global ) - jQuery.event.trigger( "ajaxSuccess", [xml, s] ); + jQuery.event.trigger( "ajaxSuccess", [xhr, s] ); } function complete(){ // Process result if ( s.complete ) - s.complete(xml, status); + s.complete(xhr, status); // The request was completed if ( s.global ) - jQuery.event.trigger( "ajaxComplete", [xml, s] ); + jQuery.event.trigger( "ajaxComplete", [xhr, s] ); // Handle the global AJAX counter if ( s.global && ! --jQuery.active ) @@ -422,51 +422,55 @@ jQuery.extend({ } // return XMLHttpRequest to allow aborting the request etc. - return xml; + return xhr; }, - handleError: function( s, xml, status, e ) { + handleError: function( s, xhr, status, e ) { // If a local callback was specified, fire it - if ( s.error ) s.error( xml, status, e ); + if ( s.error ) s.error( xhr, status, e ); // Fire the global callback if ( s.global ) - jQuery.event.trigger( "ajaxError", [xml, s, e] ); + jQuery.event.trigger( "ajaxError", [xhr, s, e] ); }, // Counter for holding the number of active queries active: 0, // Determines if an XMLHttpRequest was successful or not - httpSuccess: function( r ) { + httpSuccess: function( xhr ) { try { // IE error sometimes returns 1223 when it should be 204 so treat it as success, see #1450 - return !r.status && location.protocol == "file:" || - ( r.status >= 200 && r.status < 300 ) || r.status == 304 || r.status == 1223 || - jQuery.browser.safari && r.status == undefined; + return !xhr.status && location.protocol == "file:" || + ( xhr.status >= 200 && xhr.status < 300 ) || xhr.status == 304 || xhr.status == 1223 || + jQuery.browser.safari && xhr.status == undefined; } catch(e){} return false; }, // Determines if an XMLHttpRequest returns NotModified - httpNotModified: function( xml, url ) { + httpNotModified: function( xhr, url ) { try { - var xmlRes = xml.getResponseHeader("Last-Modified"); + var xhrRes = xhr.getResponseHeader("Last-Modified"); // Firefox always returns 200. check Last-Modified date - return xml.status == 304 || xmlRes == jQuery.lastModified[url] || - jQuery.browser.safari && xml.status == undefined; + return xhr.status == 304 || xhrRes == jQuery.lastModified[url] || + jQuery.browser.safari && xhr.status == undefined; } catch(e){} return false; }, - httpData: function( r, type ) { - var ct = r.getResponseHeader("content-type"), + httpData: function( xhr, type, filter ) { + var ct = xhr.getResponseHeader("content-type"), xml = type == "xml" || !type && ct && ct.indexOf("xml") >= 0, - data = xml ? r.responseXML : r.responseText; + data = xml ? xhr.responseXML : xhr.responseText; if ( xml && data.documentElement.tagName == "parsererror" ) throw "parsererror"; + + // Allow a pre-filtering function to sanitize the response + if( filter ) + data = filter( data, type ); // If the type is "script", eval it in global context if ( type == "script" ) @@ -482,14 +486,18 @@ jQuery.extend({ // Serialize an array of form elements or a set of // key/values into a query string param: function( a ) { - var s = []; + var s = [ ]; + + function add( key, value ){ + s[ s.length ] = encodeURIComponent(key) + '=' + encodeURIComponent(value); + }; // If an array was passed in, assume that it is an array // of form elements if ( a.constructor == Array || a.jquery ) // Serialize the form elements jQuery.each( a, function(){ - s.push( encodeURIComponent(this.name) + "=" + encodeURIComponent( this.value ) ); + add( this.name, this.value ); }); // Otherwise, assume that it's an object of key/value pairs @@ -499,10 +507,10 @@ jQuery.extend({ // If the value is an array then the key names need to be repeated if ( a[j] && a[j].constructor == Array ) jQuery.each( a[j], function(){ - s.push( encodeURIComponent(j) + "=" + encodeURIComponent( this ) ); + add( j, this ); }); else - s.push( encodeURIComponent(j) + "=" + encodeURIComponent( jQuery.isFunction(a[j]) ? a[j]() : a[j] ) ); + add( j, jQuery.isFunction(a[j]) ? a[j]() : a[j] ); // Return the resulting serialization return s.join("&").replace(/%20/g, "+");