X-Git-Url: http://git.asbjorn.it/?a=blobdiff_plain;f=src%2Fajax.js;h=3158ca44a95fb82407eaa872b86da5134958fece;hb=31949fa528141dad4b92851212677b039cb23cfb;hp=cca63a291bcc37a708544cb7f0ab0663b357a71b;hpb=02ca45573b8428f16e3eddb41f8fc50867bbda1b;p=jquery.git diff --git a/src/ajax.js b/src/ajax.js index cca63a2..3158ca4 100644 --- a/src/ajax.js +++ b/src/ajax.js @@ -7,15 +7,13 @@ var r20 = /%20/g, rheaders = /^(.*?):\s*(.*?)\r?$/mg, // IE leaves an \r character at EOL rinput = /^(?:color|date|datetime|email|hidden|month|number|password|range|search|tel|text|time|url|week)$/i, rnoContent = /^(?:GET|HEAD)$/, + rprotocol = /^\/\//, rquery = /\?/, rscript = /)<[^<]*)*<\/script>/gi, rselectTextarea = /^(?:select|textarea)/i, rspacesAjax = /\s+/, rts = /([?&])_=[^&]*/, - rurl = /^(\w+:)?\/\/([^\/?#:]+)(?::(\d+))?/, - - // Slice function - sliceFunc = Array.prototype.slice, + rurl = /^(\w+:)\/\/([^\/?#:]+)(?::(\d+))?/, // Keep a copy of the old load method _load = jQuery.fn.load, @@ -50,7 +48,7 @@ function addToPrefiltersOrTransports( structure ) { } if ( jQuery.isFunction( func ) ) { - var dataTypes = dataTypeExpression.split( rspacesAjax ), + var dataTypes = dataTypeExpression.toLowerCase().split( rspacesAjax ), i = 0, length = dataTypes.length, dataType, @@ -64,7 +62,7 @@ function addToPrefiltersOrTransports( structure ) { // any existing element placeBefore = /^\+/.test( dataType ); if ( placeBefore ) { - dataType = dataType.substr( 1 ); + dataType = dataType.substr( 1 ) || "*"; } list = structure[ dataType ] = structure[ dataType ] || []; // then we add to the structure accordingly @@ -75,39 +73,43 @@ function addToPrefiltersOrTransports( structure ) { } //Base inspection function for prefilters and transports -function inspectPrefiltersOrTransports( structure, options, originalOptions, - dataType /* internal */, tested /* internal */ ) { +function inspectPrefiltersOrTransports( structure, options, originalOptions, jXHR, + dataType /* internal */, inspected /* internal */ ) { dataType = dataType || options.dataTypes[ 0 ]; - tested = tested || {}; + inspected = inspected || {}; - tested[ dataType ] = true; + inspected[ dataType ] = true; var list = structure[ dataType ], i = 0, length = list ? list.length : 0, - executeOnly = structure === prefilters, - selected; - - for(; i < length && !( executeOnly ? typeof selected === "string" && !tested[ selected ] : selected ); i++ ) { - selected = list[ i ]( options, originalOptions ); + executeOnly = ( structure === prefilters ), + selection; + + for(; i < length && ( executeOnly || !selection ); i++ ) { + selection = list[ i ]( options, originalOptions, jXHR ); + // If we got redirected to another dataType + // we try there if not done already + if ( typeof selection === "string" ) { + if ( inspected[ selection ] ) { + selection = undefined; + } else { + options.dataTypes.unshift( selection ); + selection = inspectPrefiltersOrTransports( + structure, options, originalOptions, jXHR, selection, inspected ); + } + } } - // If we got redirected to another dataType - // we try there - if ( typeof selected === "string" && !tested[ selected ] ) { - options.dataTypes.unshift( selected ); - selected = inspectPrefiltersOrTransports( - structure, options, originalOptions, selected, tested ); - // If we're only executing or nothing was selected // we try the catchall dataType if not done already - } else if ( ( executeOnly || !selected ) && !tested[ "*" ] ) { - selected = inspectPrefiltersOrTransports( - structure, options, originalOptions, "*" ,tested ); + if ( ( executeOnly || !selection ) && !inspected[ "*" ] ) { + selection = inspectPrefiltersOrTransports( + structure, options, originalOptions, jXHR, "*", inspected ); } // unnecessary when only executing (prefilters) // but it'll be ignored by the caller in that case - return selected; + return selection; } jQuery.fn.extend({ @@ -320,9 +322,8 @@ jQuery.extend({ // Main method ajax: function( url, options ) { - // If options is not an object, - // we simulate pre-1.5 signature - if ( typeof options !== "object" ) { + // If url is an object, simulate pre-1.5 signature + if ( typeof url === "object" ) { options = url; url = undefined; } @@ -332,18 +333,24 @@ jQuery.extend({ var // Create the final options object s = jQuery.extend( true, {}, jQuery.ajaxSettings, options ), - // Callbacks contexts + // Callbacks context // We force the original context if it exists // or take it from jQuery.ajaxSettings otherwise // (plain objects used as context get extended) callbackContext = ( s.context = ( "context" in options ? options : jQuery.ajaxSettings ).context ) || s, - globalEventContext = callbackContext === s ? jQuery.event : jQuery( callbackContext ), + // Context for global events + // It's the callbackContext if one was provided in the options + // and if it's a DOM node + globalEventContext = callbackContext !== s && callbackContext.nodeType ? + jQuery( callbackContext ) : jQuery.event, // Deferreds deferred = jQuery.Deferred(), completeDeferred = jQuery._Deferred(), // Status-dependent callbacks statusCode = s.statusCode || {}, + // ifModified key + ifModifiedKey, // Headers (they are sent all at once) requestHeaders = {}, // Response headers @@ -396,8 +403,9 @@ jQuery.extend({ // Cancel the request abort: function( statusText ) { + statusText = statusText || "abort"; if ( transport ) { - transport.abort( statusText || "abort" ); + transport.abort( statusText ); } done( 0, statusText ); return this; @@ -434,7 +442,7 @@ jQuery.extend({ var isSuccess, success, - error = ( statusText = statusText || "error" ), + error, response = responses ? ajaxHandleResponses( s, jXHR, responses ) : undefined, lastModified, etag; @@ -446,10 +454,10 @@ jQuery.extend({ if ( s.ifModified ) { if ( ( lastModified = jXHR.getResponseHeader( "Last-Modified" ) ) ) { - jQuery.lastModified[ s.url ] = lastModified; + jQuery.lastModified[ ifModifiedKey ] = lastModified; } if ( ( etag = jXHR.getResponseHeader( "Etag" ) ) ) { - jQuery.etag[ s.url ] = etag; + jQuery.etag[ ifModifiedKey ] = etag; } } @@ -469,7 +477,17 @@ jQuery.extend({ } catch(e) { // We have a parsererror statusText = "parsererror"; - error = "" + e; + error = e; + } + } + } else { + // We extract error from statusText + // then normalize statusText and status for non-aborts + error = statusText; + if( status ) { + statusText = "error"; + if ( status < 0 ) { + status = 0; } } } @@ -529,8 +547,9 @@ jQuery.extend({ }; // Remove hash character (#7531: and string promotion) + // Add protocol if not provided (#5866: IE7 issue with protocol-less urls) // We also use the url parameter if available - s.url = ( "" + ( url || s.url ) ).replace( rhash, "" ); + s.url = ( "" + ( url || s.url ) ).replace( rhash, "" ).replace( rprotocol, protocol + "//" ); // Extract dataTypes list s.dataTypes = jQuery.trim( s.dataType || "*" ).toLowerCase().split( rspacesAjax ); @@ -538,12 +557,10 @@ jQuery.extend({ // Determine if a cross-domain request is in order if ( !s.crossDomain ) { parts = rurl.exec( s.url.toLowerCase() ); - s.crossDomain = !!( - parts && - ( parts[ 1 ] && parts[ 1 ] != protocol || - parts[ 2 ] != loc.hostname || - ( parts[ 3 ] || ( ( parts[ 1 ] || protocol ) === "http:" ? 80 : 443 ) ) != - ( loc.port || ( protocol === "http:" ? 80 : 443 ) ) ) + s.crossDomain = !!( parts && + ( parts[ 1 ] != protocol || parts[ 2 ] != loc.hostname || + ( parts[ 3 ] || ( parts[ 1 ] === "http:" ? 80 : 443 ) ) != + ( loc.port || ( protocol === "http:" ? 80 : 443 ) ) ) ); } @@ -553,7 +570,7 @@ jQuery.extend({ } // Apply prefilters - inspectPrefiltersOrTransports( prefilters, s, options ); + inspectPrefiltersOrTransports( prefilters, s, options, jXHR ); // Uppercase the type s.type = s.type.toUpperCase(); @@ -574,6 +591,9 @@ jQuery.extend({ s.url += ( rquery.test( s.url ) ? "&" : "?" ) + s.data; } + // Get ifModifiedKey before adding the anti-cache parameter + ifModifiedKey = s.url; + // Add anti-cache in url if needed if ( s.cache === false ) { @@ -593,11 +613,12 @@ jQuery.extend({ // Set the If-Modified-Since and/or If-None-Match header, if in ifModified mode. if ( s.ifModified ) { - if ( jQuery.lastModified[ s.url ] ) { - requestHeaders[ "if-modified-since" ] = jQuery.lastModified[ s.url ]; + ifModifiedKey = ifModifiedKey || s.url; + if ( jQuery.lastModified[ ifModifiedKey ] ) { + requestHeaders[ "if-modified-since" ] = jQuery.lastModified[ ifModifiedKey ]; } - if ( jQuery.etag[ s.url ] ) { - requestHeaders[ "if-none-match" ] = jQuery.etag[ s.url ]; + if ( jQuery.etag[ ifModifiedKey ] ) { + requestHeaders[ "if-none-match" ] = jQuery.etag[ ifModifiedKey ]; } } @@ -626,11 +647,11 @@ jQuery.extend({ } // Get transport - transport = inspectPrefiltersOrTransports( transports, s, options ); + transport = inspectPrefiltersOrTransports( transports, s, options, jXHR ); // If no transport, we auto-abort if ( !transport ) { - done( 0, "notransport" ); + done( -1, "No Transport" ); } else { // Set state as sending state = jXHR.readyState = 1; @@ -649,9 +670,8 @@ jQuery.extend({ transport.send( requestHeaders, done ); } catch (e) { // Propagate exception as error if not done - if ( status === 1 ) { - done( 0, "error", "" + e ); - jXHR = false; + if ( status < 2 ) { + done( -1, e ); // Simply rethrow otherwise } else { jQuery.error( e ); @@ -725,9 +745,9 @@ function buildParams( prefix, obj, traditional, add ) { // Serialize object item. } else { - jQuery.each( obj, function( k, v ) { - buildParams( prefix + "[" + k + "]", v, traditional, add ); - }); + for ( var name in obj ) { + buildParams( prefix + "[" + name + "]", obj[ name ], traditional, add ); + } } } else { @@ -827,8 +847,9 @@ function ajaxConvert( s, response ) { } var dataTypes = s.dataTypes, - converters = s.converters, + converters = {}, i, + key, length = dataTypes.length, tmp, // Current and previous dataTypes @@ -838,13 +859,23 @@ function ajaxConvert( s, response ) { conversion, // Conversion function conv, - // Conversion functions (when text is used in-between) + // Conversion functions (transitive conversion) conv1, conv2; // For each dataType in the chain for( i = 1; i < length; i++ ) { + // Create converters map + // with lowercased keys + if ( i === 1 ) { + for( key in s.converters ) { + if( typeof key === "string" ) { + converters[ key.toLowerCase() ] = s.converters[ key ]; + } + } + } + // Get the dataTypes prev = current; current = dataTypes[ i ];