mirror of
https://github.com/jquery/jquery.git
synced 2024-11-23 02:54:22 +00:00
Ajax: Golf away 21 bytes
Close gh-2699
This commit is contained in:
parent
e0c25abb43
commit
eaa3e9f0cf
58
src/ajax.js
58
src/ajax.js
@ -406,6 +406,9 @@ jQuery.extend( {
|
|||||||
// Url cleanup var
|
// Url cleanup var
|
||||||
urlAnchor,
|
urlAnchor,
|
||||||
|
|
||||||
|
// Request state (becomes false upon send and true upon completion)
|
||||||
|
completed,
|
||||||
|
|
||||||
// To know if global events are to be dispatched
|
// To know if global events are to be dispatched
|
||||||
fireGlobals,
|
fireGlobals,
|
||||||
|
|
||||||
@ -435,9 +438,6 @@ jQuery.extend( {
|
|||||||
requestHeaders = {},
|
requestHeaders = {},
|
||||||
requestHeadersNames = {},
|
requestHeadersNames = {},
|
||||||
|
|
||||||
// The jqXHR state
|
|
||||||
state = 0,
|
|
||||||
|
|
||||||
// Default abort message
|
// Default abort message
|
||||||
strAbort = "canceled",
|
strAbort = "canceled",
|
||||||
|
|
||||||
@ -448,7 +448,7 @@ jQuery.extend( {
|
|||||||
// Builds headers hashtable if needed
|
// Builds headers hashtable if needed
|
||||||
getResponseHeader: function( key ) {
|
getResponseHeader: function( key ) {
|
||||||
var match;
|
var match;
|
||||||
if ( state === 2 ) {
|
if ( completed ) {
|
||||||
if ( !responseHeaders ) {
|
if ( !responseHeaders ) {
|
||||||
responseHeaders = {};
|
responseHeaders = {};
|
||||||
while ( ( match = rheaders.exec( responseHeadersString ) ) ) {
|
while ( ( match = rheaders.exec( responseHeadersString ) ) ) {
|
||||||
@ -462,14 +462,14 @@ jQuery.extend( {
|
|||||||
|
|
||||||
// Raw string
|
// Raw string
|
||||||
getAllResponseHeaders: function() {
|
getAllResponseHeaders: function() {
|
||||||
return state === 2 ? responseHeadersString : null;
|
return completed ? responseHeadersString : null;
|
||||||
},
|
},
|
||||||
|
|
||||||
// Caches the header
|
// Caches the header
|
||||||
setRequestHeader: function( name, value ) {
|
setRequestHeader: function( name, value ) {
|
||||||
var lname = name.toLowerCase();
|
if ( completed == null ) {
|
||||||
if ( !state ) {
|
name = requestHeadersNames[ name.toLowerCase() ] =
|
||||||
name = requestHeadersNames[ lname ] = requestHeadersNames[ lname ] || name;
|
requestHeadersNames[ name.toLowerCase() ] || name;
|
||||||
requestHeaders[ name ] = value;
|
requestHeaders[ name ] = value;
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
@ -477,7 +477,7 @@ jQuery.extend( {
|
|||||||
|
|
||||||
// Overrides response content-type header
|
// Overrides response content-type header
|
||||||
overrideMimeType: function( type ) {
|
overrideMimeType: function( type ) {
|
||||||
if ( !state ) {
|
if ( completed == null ) {
|
||||||
s.mimeType = type;
|
s.mimeType = type;
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
@ -487,16 +487,16 @@ jQuery.extend( {
|
|||||||
statusCode: function( map ) {
|
statusCode: function( map ) {
|
||||||
var code;
|
var code;
|
||||||
if ( map ) {
|
if ( map ) {
|
||||||
if ( state < 2 ) {
|
if ( completed ) {
|
||||||
for ( code in map ) {
|
|
||||||
|
|
||||||
// Lazy-add the new callback in a way that preserves old ones
|
|
||||||
statusCode[ code ] = [ statusCode[ code ], map[ code ] ];
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
|
|
||||||
// Execute the appropriate callbacks
|
// Execute the appropriate callbacks
|
||||||
jqXHR.always( map[ jqXHR.status ] );
|
jqXHR.always( map[ jqXHR.status ] );
|
||||||
|
} else {
|
||||||
|
|
||||||
|
// Lazy-add the new callbacks in a way that preserves old ones
|
||||||
|
for ( code in map ) {
|
||||||
|
statusCode[ code ] = [ statusCode[ code ], map[ code ] ];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
@ -560,7 +560,7 @@ jQuery.extend( {
|
|||||||
inspectPrefiltersOrTransports( prefilters, s, options, jqXHR );
|
inspectPrefiltersOrTransports( prefilters, s, options, jqXHR );
|
||||||
|
|
||||||
// If request was aborted inside a prefilter, stop there
|
// If request was aborted inside a prefilter, stop there
|
||||||
if ( state === 2 ) {
|
if ( completed ) {
|
||||||
return jqXHR;
|
return jqXHR;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -642,7 +642,7 @@ jQuery.extend( {
|
|||||||
|
|
||||||
// Allow custom headers/mimetypes and early abort
|
// Allow custom headers/mimetypes and early abort
|
||||||
if ( s.beforeSend &&
|
if ( s.beforeSend &&
|
||||||
( s.beforeSend.call( callbackContext, jqXHR, s ) === false || state === 2 ) ) {
|
( s.beforeSend.call( callbackContext, jqXHR, s ) === false || completed ) ) {
|
||||||
|
|
||||||
// Abort if not done already and return
|
// Abort if not done already and return
|
||||||
return jqXHR.abort();
|
return jqXHR.abort();
|
||||||
@ -671,7 +671,7 @@ jQuery.extend( {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If request was aborted inside ajaxSend, stop there
|
// If request was aborted inside ajaxSend, stop there
|
||||||
if ( state === 2 ) {
|
if ( completed ) {
|
||||||
return jqXHR;
|
return jqXHR;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -683,18 +683,17 @@ jQuery.extend( {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
state = 1;
|
completed = false;
|
||||||
transport.send( requestHeaders, done );
|
transport.send( requestHeaders, done );
|
||||||
} catch ( e ) {
|
} catch ( e ) {
|
||||||
|
|
||||||
// Propagate exception as error if not done
|
// Rethrow post-completion exceptions
|
||||||
if ( state < 2 ) {
|
if ( completed ) {
|
||||||
done( -1, e );
|
|
||||||
|
|
||||||
// Simply rethrow otherwise
|
|
||||||
} else {
|
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Propagate others as results
|
||||||
|
done( -1, e );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -703,13 +702,12 @@ jQuery.extend( {
|
|||||||
var isSuccess, success, error, response, modified,
|
var isSuccess, success, error, response, modified,
|
||||||
statusText = nativeStatusText;
|
statusText = nativeStatusText;
|
||||||
|
|
||||||
// Called once
|
// Ignore repeat invocations
|
||||||
if ( state === 2 ) {
|
if ( completed ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// State is "done" now
|
completed = true;
|
||||||
state = 2;
|
|
||||||
|
|
||||||
// Clear timeout if it exists
|
// Clear timeout if it exists
|
||||||
if ( timeoutTimer ) {
|
if ( timeoutTimer ) {
|
||||||
|
Loading…
Reference in New Issue
Block a user