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\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/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,
*/
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, jXHR,
+ 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, 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'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, jXHR, "*", 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 ) {
}
},
- 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 ) {
// If options is not an object,
// we simulate pre-1.5 signature
- if ( typeof( options ) !== "object" ) {
+ if ( typeof options !== "object" ) {
options = url;
url = undefined;
}
// Cancel the request
abort: function( statusText ) {
+ statusText = statusText || "abort";
if ( transport ) {
- transport.abort( statusText || "abort" );
+ transport.abort( statusText );
}
done( 0, statusText );
return this;
var isSuccess,
success,
- error = ( statusText = statusText || "error" ),
+ error,
response = responses ? ajaxHandleResponses( s, jXHR, responses ) : undefined,
lastModified,
etag;
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
statusCode = undefined;
if ( s.global ) {
- globalEventContext.trigger( "ajax" + ( isSuccess ? "Success" : "Error" ) ,
+ globalEventContext.trigger( "ajax" + ( isSuccess ? "Success" : "Error" ),
[ jXHR, s, isSuccess ? success : error ] );
}
if ( s.global ) {
globalEventContext.trigger( "ajaxComplete", [ jXHR, s] );
// Handle the global AJAX counter
- if ( ! --jQuery.active ) {
+ if ( !( --jQuery.active ) ) {
jQuery.event.trigger( "ajaxStop" );
}
}
}
} else {
tmp = map[ jXHR.status ];
- jXHR.then( tmp , tmp );
+ jXHR.then( tmp, tmp );
}
}
return this;
};
// 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 );
// 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 ) ) )
);
}
}
// Apply prefilters
- jQuery.ajaxPrefilter( s, options );
+ inspectPrefiltersOrTransports( prefilters, s, options, jXHR );
// Uppercase the type
s.type = s.type.toUpperCase();
} 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, 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;
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 );
});
-// 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)
} else {
// Try convertible dataTypes
for ( type in responses ) {
- if ( ! dataTypes[ 0 ] || s.converters[ type + " " + dataTypes[0] ] ) {
+ if ( !dataTypes[ 0 ] || s.converters[ type + " " + dataTypes[0] ] ) {
finalDataType = type;
break;
}
- if ( ! firstDataType ) {
+ if ( !firstDataType ) {
firstDataType = type;
}
}
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( " " );
}
}
// 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