X-Git-Url: http://git.asbjorn.biz/?p=jquery.git;a=blobdiff_plain;f=src%2Fajax.js;h=bf0259cfddee632357f39b7950ee3e354d5fc559;hp=fc1ecfde373a9fbc4e99e4d5850aeb4a14444572;hb=3dbd600e1961d9ab5296afbf2b3eb0852ad176e0;hpb=64e1cdbb95b8bbefbc9dec70ae30e0714a549619 diff --git a/src/ajax.js b/src/ajax.js index fc1ecfd..bf0259c 100644 --- a/src/ajax.js +++ b/src/ajax.js @@ -199,6 +199,11 @@ jQuery.extend({ json: /json/ }, + responseFields: { + xml: "responseXML", + text: "responseText" + }, + // Prefilters // 1) They are useful to introduce custom dataTypes (see transport/jsonp for an example) // 2) These are called: @@ -231,53 +236,7 @@ jQuery.extend({ // Parse text as xml "text xml": jQuery.parseXML - }, - - // Utility function that handles dataType when response is received - // (for those transports that can give text or xml responses) - determineDataType: function( ct , text , xml ) { - - var s = this, - contents = s.contents, - type, - regexp, - dataTypes = s.dataTypes, - transportDataType = dataTypes[0], - response; - - // Auto (xml, json, script or text determined given headers) - if ( transportDataType === "*" ) { - - for ( type in contents ) { - if ( ( regexp = contents[ type ] ) && regexp.test( ct ) ) { - transportDataType = dataTypes[0] = type; - break; - } - } - } - - // xml and parsed as such - if ( transportDataType === "xml" && - xml && - xml.documentElement /* #4958 */ ) { - - response = xml; - - // Text response was provided - } else { - - response = text; - - // If it's not really text, defer to converters - if ( transportDataType !== "text" ) { - dataTypes.unshift( "text" ); - } - - } - - return response; } - }, ajaxPrefilter: function( a , b ) { @@ -291,18 +250,16 @@ jQuery.extend({ // Main method ajax: function( url , options ) { - // Handle varargs - if ( arguments.length === 1 ) { + // If options is not an object, + // we simulate pre-1.5 signature + if ( typeof( options ) !== "object" ) { options = url; - url = options ? options.url : undefined; + url = undefined; } // Force options to be an object options = options || {}; - // Get the url if provided separately - options.url = url || options.url; - var // Create the final options object s = jQuery.extend( true , {} , jQuery.ajaxSettings , options ), // jQuery lists @@ -390,7 +347,7 @@ jQuery.extend({ // Callback for when everything is done // It is defined here because jslint complains if it is declared // at the end of the function (which would be more logical and readable) - function done( status , statusText , response , headers) { + function done( status , statusText , responses , headers) { // Called once if ( state === 2 ) { @@ -401,7 +358,7 @@ jQuery.extend({ state = 2; // Dereference transport for early garbage collection - // (no matter how long the jXHR transport will be used + // (no matter how long the jXHR object will be used) transport = undefined; // Set readyState @@ -415,22 +372,73 @@ jQuery.extend({ clearTimeout(timeoutTimer); } - var // Reference url - url = s.url, - // and ifModified status - ifModified = s.ifModified, + var // Reference dataTypes and responseFields + dataTypes = s.dataTypes, + responseFields = s.responseFields, + responseField, - // Is it a success? - isSuccess = 0, + // Flag to mark as success + isSuccess, // Stored success success, // Stored error error, - // Keep track of statusCode callbacks - oldStatusCode = statusCode; + // To keep track of statusCode based callbacks + oldStatusCode, - statusCode = undefined; + // Actual response + response; + + // If we got responses: + // - find the right one + // - update dataTypes accordingly + // - set responseXXX accordingly too + if ( responses ) { + + var contents = s.contents, + transportDataType = dataTypes[0], + ct, + type, + finalDataType; + + // Auto (xml, json, script or text determined given headers) + if ( transportDataType === "*" && ( ct = jXHR.getResponseHeader( "content-type" ) ) ) { + + for ( type in contents ) { + if ( contents[ type ] && contents[ type ].test( ct ) ) { + transportDataType = dataTypes[0] = type; + break; + } + } + + type = undefined; + } + + // Get final dataType + for( type in responses ) { + if ( ! finalDataType && type === transportDataType ) { + finalDataType = type; + } + responseField = responseFields[ type ]; + if ( responseField && ! ( responseField in jXHR ) ) { + jXHR[ responseField ] = responses[ type ]; + } + } + + // If no response with the expected dataType was provided + // Take the last response as a default if it exists + if ( ! finalDataType && type ) { + finalDataType = type; + if ( transportDataType === "*" ) { + dataTypes.shift(); + } + dataTypes.unshift( finalDataType ); + } + + // Get final response + response = responses[ finalDataType ]; + } // If successful, handle type chaining if ( status >= 200 && status < 300 || status === 304 ) { @@ -452,15 +460,12 @@ jQuery.extend({ // If not modified if ( status === 304 ) { - // Set the statusText accordingly statusText = "notmodified"; - // Mark as a success isSuccess = 1; // If we have data } else { - // Set the statusText accordingly statusText = "success"; // Chain data conversions and determine the final value @@ -479,14 +484,8 @@ jQuery.extend({ // Conversion functions (when text is used in-between) conv1, conv2, - // Local references to dataTypes & converters - dataTypes = s.dataTypes, - converters = s.converters, - // DataType to responseXXX field mapping - responses = { - "xml": "XML", - "text": "Text" - }; + // Local references to converters + converters = s.converters; // For each dataType in the chain for( i = 0 ; i < dataTypes.length ; i++ ) { @@ -495,11 +494,9 @@ jQuery.extend({ // If a responseXXX field for this dataType exists // and if it hasn't been set yet - if ( responses[ current ] ) { - // Set it - jXHR[ "response" + responses[ current ] ] = response; - // Mark it as set - responses[ current ] = 0; + responseField = responseFields[ current ]; + if ( responseField && ! ( responseField in jXHR ) ) { + jXHR[ responseField ] = response; } // If this is not the first element @@ -567,13 +564,7 @@ jQuery.extend({ // if not success, mark it as an error } else { - error = statusText = statusText || "error"; - - // Set responseText if needed - if ( response ) { - jXHR.responseText = response; - } } // Set data for the fake xhr object @@ -582,12 +573,14 @@ jQuery.extend({ // Success/Error if ( isSuccess ) { - deferred.fire( callbackContext , [ success , statusText , jXHR ] ); + deferred.resolveWith( callbackContext , [ success , statusText , jXHR ] ); } else { - deferred.fireReject( callbackContext , [ jXHR , statusText , error ] ); + deferred.rejectWith( callbackContext , [ jXHR , statusText , error ] ); } // Status-dependent callbacks + oldStatusCode = statusCode; + statusCode = undefined; jXHR.statusCode( oldStatusCode ); if ( s.global ) { @@ -596,7 +589,7 @@ jQuery.extend({ } // Complete - completeDeferred.fire( callbackContext, [ jXHR , statusText ] ); + completeDeferred.resolveWith( callbackContext, [ jXHR , statusText ] ); if ( s.global ) { globalEventContext.trigger( "ajaxComplete" , [ jXHR , s] ); @@ -630,7 +623,8 @@ jQuery.extend({ }; // Remove hash character (#7531: and string promotion) - s.url = ( "" + s.url ).replace( rhash , "" ); + // We also use the url parameter if available + s.url = ( "" + ( url || s.url ) ).replace( rhash , "" ); // Extract dataTypes list s.dataTypes = jQuery.trim( s.dataType || "*" ).toLowerCase().split( /\s+/ );