X-Git-Url: http://git.asbjorn.biz/?p=jquery.git;a=blobdiff_plain;f=src%2Fajax.js;h=8f8bc60ab07fc1105ff4a1377857fdb8dc238854;hp=bc3696ee8e5c9e133a28b89fcb0517bd18aa80bf;hb=5ca8f0617f5c94495380ff783452a52eab706d39;hpb=ee22c8b34bb801f26cacc67a1a3a0ac595fe7928 diff --git a/src/ajax.js b/src/ajax.js index bc3696e..8f8bc60 100644 --- a/src/ajax.js +++ b/src/ajax.js @@ -38,6 +38,82 @@ var r20 = /%20/g, */ transports = {}; +// Base "constructor" for jQuery.ajaxPrefilter and jQuery.ajaxTransport +function addToPrefiltersOrTransports( structure ) { + + // dataTypeExpression is optional and defaults to "*" + return function( dataTypeExpression, func ) { + + if ( typeof dataTypeExpression !== "string" ) { + func = dataTypeExpression; + dataTypeExpression = "*"; + } + + if ( jQuery.isFunction( func ) ) { + var dataTypes = dataTypeExpression.split( rspacesAjax ), + i = 0, + length = dataTypes.length, + dataType, + list, + placeBefore; + + // For each dataType in the dataTypeExpression + for(; i < length; i++ ) { + dataType = dataTypes[ i ]; + // We control if we're asked to add before + // any existing element + placeBefore = /^\+/.test( dataType ); + if ( placeBefore ) { + dataType = dataType.substr( 1 ); + } + list = structure[ dataType ] = structure[ dataType ] || []; + // then we add to the structure accordingly + list[ placeBefore ? "unshift" : "push" ]( func ); + } + } + }; +} + +//Base inspection function for prefilters and transports +function inspectPrefiltersOrTransports( structure, options, originalOptions, + dataType /* internal */, inspected /* internal */ ) { + + dataType = dataType || options.dataTypes[ 0 ]; + inspected = inspected || {}; + + inspected[ dataType ] = true; + + var list = structure[ dataType ], + i = 0, + length = list ? list.length : 0, + executeOnly = ( structure === prefilters ), + selection; + + for(; i < length && ( executeOnly || !selection ); i++ ) { + selection = list[ i ]( options, originalOptions ); + // 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, selection, inspected ); + } + } + } + // If we're only executing or nothing was selected + // we try the catchall dataType if not done already + if ( ( executeOnly || !selection ) && !inspected[ "*" ] ) { + selection = inspectPrefiltersOrTransports( + structure, options, originalOptions, "*", inspected ); + } + // unnecessary when only executing (prefilters) + // but it'll be ignored by the caller in that case + return selection; +} + jQuery.fn.extend({ load: function( url, params, callback ) { if ( typeof url !== "string" && _load ) { @@ -242,13 +318,8 @@ jQuery.extend({ } }, - ajaxPrefilter: function( a, b ) { - prefiltersOrTransports( prefilters, a, b ); - }, - - ajaxTransport: function( a, b ) { - return prefiltersOrTransports( transports, a, b ); - }, + ajaxPrefilter: addToPrefiltersOrTransports( prefilters ), + ajaxTransport: addToPrefiltersOrTransports( transports ), // Main method ajax: function( url, options ) { @@ -329,8 +400,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; @@ -367,7 +439,7 @@ jQuery.extend({ var isSuccess, success, - error = ( statusText = statusText || "error" ), + error, response = responses ? ajaxHandleResponses( s, jXHR, responses ) : undefined, lastModified, etag; @@ -405,6 +477,16 @@ jQuery.extend({ 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; + } + } } // Set data for the fake xhr object @@ -433,7 +515,7 @@ jQuery.extend({ if ( s.global ) { globalEventContext.trigger( "ajaxComplete", [ jXHR, s] ); // Handle the global AJAX counter - if ( ! --jQuery.active ) { + if ( !( --jQuery.active ) ) { jQuery.event.trigger( "ajaxStop" ); } } @@ -486,7 +568,7 @@ jQuery.extend({ } // Apply prefilters - jQuery.ajaxPrefilter( s, options ); + inspectPrefiltersOrTransports( prefilters, s, options ); // Uppercase the type s.type = s.type.toUpperCase(); @@ -554,16 +636,16 @@ jQuery.extend({ } else { // Install callbacks on deferreds - for ( i in { success:1, error:1, complete:1 } ) { + for ( i in { success: 1, error: 1, complete: 1 } ) { jXHR[ i ]( s[ i ] ); } // Get transport - transport = jQuery.ajaxTransport( s, options ); + transport = inspectPrefiltersOrTransports( transports, s, options ); // If no transport, we auto-abort if ( !transport ) { - done( 0, "notransport" ); + done( -1, "No Transport" ); } else { // Set state as sending state = jXHR.readyState = 1; @@ -582,9 +664,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 ); @@ -682,96 +763,6 @@ jQuery.extend({ }); -// Base inspection function for prefilters and transports -function inspectPrefiltersOrTransports( structure, options, originalOptions, dataType, tested ) { - - dataType = dataType || options.dataTypes[ 0 ]; - tested = tested || {}; - - if ( !tested[ dataType ] ) { - - tested[ dataType ] = true; - - var list = structure[ dataType ], - i = 0, - length = list ? list.length : 0, - executeOnly = structure === prefilters, - selected; - - for(; ( executeOnly || !selected ) && i < length; i++ ) { - selected = list[ i ]( options, originalOptions ); - // If we got redirected to a different dataType, - // we add it and switch to the corresponding list - if ( typeof selected === "string" && selected !== dataType ) { - options.dataTypes.unshift( selected ); - selected = inspectPrefiltersOrTransports( - structure, options, originalOptions, selected, tested ); - // We always break in order not to continue - // to iterate in previous list - break; - } - } - // If we're only executing or nothing was selected - // we try the catchall dataType - if ( !tested[ "*" ] && ( executeOnly || ! selected ) ) { - selected = inspectPrefiltersOrTransports( - structure, options, originalOptions, "*" ,tested ); - } - // This will be ignored by ajaxPrefilter - // so it's safe to return no matter what - return selected; - } -} - -function addToPrefiltersOrTransports( structure, dataTypeExpression, functor ) { - - var dataTypes = dataTypeExpression.split( rspacesAjax ), - i = 0, - length = dataTypes.length, - dataType, - list, - placeBefore; - - // For each dataType in the dataTypeExpression - for(; i < length; i++ ) { - dataType = dataTypes[ i ]; - // We control if we're asked to add before - // any existing element - placeBefore = /^\+/.test( dataType ); - if ( placeBefore ) { - dataType = dataType.substr( 1 ); - } - list = structure[ dataType ] = structure[ dataType ] || []; - // then we add to the structure accordingly - list[ placeBefore ? "unshift" : "push" ]( functor ); - } -} - -// Base function for both ajaxPrefilter and ajaxTransport -function prefiltersOrTransports( structure, arg1, arg2, type /* internal */ ) { - - type = jQuery.type( arg1 ); - - if ( type === "object" ) { - // We have an options map so we have to inspect the structure - return inspectPrefiltersOrTransports( structure, arg1, arg2 ); - } else { - // We're requested to add to the structure - // Signature is ( dataTypeExpression, function ) - // with dataTypeExpression being optional and - // defaulting to auto ("*") - type = ( type === "function" ); - if ( type ) { - arg2 = arg1; - arg1 = undefined; - } - // We control that the second argument is really a function - if ( type || jQuery.isFunction( arg2 ) ) { - addToPrefiltersOrTransports( structure, arg1 || "*", arg2 ); - } - } -} - /* Handles responses to an ajax request: * - sets all responseXXX fields accordingly * - finds the right dataType (mediates between content-type and expected dataType) @@ -883,7 +874,7 @@ function ajaxConvert( s, response ) { conv = converters[ conversion ] || converters[ "* " + current ]; // If there is no direct converter, search transitively - if ( ! conv ) { + if ( !conv ) { conv2 = undefined; for( conv1 in converters ) { tmp = conv1.split( " " ); @@ -902,7 +893,7 @@ function ajaxConvert( s, response ) { } } // If we found no converter, dispatch an error - if ( ! ( conv || conv2 ) ) { + if ( !( conv || conv2 ) ) { jQuery.error( "No conversion from " + conversion.replace(" "," to ") ); } // If found converter is not an equivalence