jquery/src/ajax/xhr.js

197 lines
4.5 KiB
JavaScript
Raw Normal View History

(function( jQuery ) {
var // Next fake timer id
xhrPollingId = jQuery.now(),
2011-01-05 22:41:23 +01:00
// Callbacks hashtable
xhrs = {},
// XHR pool
xhrPool = [],
// #5280: see end of file
xhrUnloadAbortMarker = [];
2011-01-05 22:41:23 +01:00
jQuery.ajax.transport( function( s , determineDataType ) {
2011-01-05 22:41:23 +01:00
// Cross domain only allowed if supported through XMLHttpRequest
if ( ! s.crossDomain || jQuery.support.cors ) {
2011-01-05 22:41:23 +01:00
var callback;
2011-01-05 22:41:23 +01:00
return {
2011-01-05 22:41:23 +01:00
send: function(headers, complete) {
2011-01-05 22:41:23 +01:00
var xhr = xhrPool.pop() || s.xhr(),
handle;
2011-01-05 22:41:23 +01:00
// Open the socket
// Passing null username, generates a login popup on Opera (#2865)
if ( s.username ) {
xhr.open(s.type, s.url, s.async, s.username, s.password);
} else {
xhr.open(s.type, s.url, s.async);
}
2011-01-05 22:41:23 +01:00
// Requested-With header
// Not set for crossDomain requests with no content
// (see why at http://trac.dojotoolkit.org/ticket/9486)
// Won't change header if already provided in beforeSend
if ( ! ( s.crossDomain && ! s.hasContent ) && ! headers["x-requested-with"] ) {
headers["x-requested-with"] = "XMLHttpRequest";
}
2011-01-05 22:41:23 +01:00
// Need an extra try/catch for cross domain requests in Firefox 3
try {
2011-01-05 22:41:23 +01:00
jQuery.each(headers, function(key,value) {
xhr.setRequestHeader(key,value);
});
2011-01-05 22:41:23 +01:00
} catch(_) {}
2011-01-05 22:41:23 +01:00
// Do send the request
try {
xhr.send( ( s.hasContent && s.data ) || null );
} catch(e) {
// Store back in pool
xhrPool.push( xhr );
complete(0, "error", "" + e);
return;
}
2011-01-05 22:41:23 +01:00
// Listener
callback = function ( abortStatusText ) {
2011-01-05 22:41:23 +01:00
// Was never called and is aborted or complete
if ( callback && ( abortStatusText || xhr.readyState === 4 ) ) {
2011-01-05 22:41:23 +01:00
// Do not listen anymore
// and Store back in pool
if (handle) {
xhr.onreadystatechange = jQuery.noop;
delete xhrs[ handle ];
handle = undefined;
xhrPool.push( xhr );
}
2011-01-05 22:41:23 +01:00
callback = 0;
2011-01-05 22:41:23 +01:00
// Get info
var status, statusText, response, responseHeaders;
2011-01-05 22:41:23 +01:00
if ( abortStatusText ) {
2011-01-05 22:41:23 +01:00
if ( xhr.readyState !== 4 ) {
xhr.abort();
}
2011-01-05 22:41:23 +01:00
// Stop here if unloadAbort
if ( abortStatusText === xhrUnloadAbortMarker ) {
return;
}
2011-01-05 22:41:23 +01:00
status = 0;
statusText = abortStatusText;
2011-01-05 22:41:23 +01:00
} else {
2011-01-05 22:41:23 +01:00
status = xhr.status;
2011-01-05 22:41:23 +01:00
try { // Firefox throws an exception when accessing statusText for faulty cross-domain requests
2011-01-05 22:41:23 +01:00
statusText = xhr.statusText;
2011-01-05 22:41:23 +01:00
} catch( e ) {
2011-01-05 22:41:23 +01:00
statusText = ""; // We normalize with Webkit giving an empty statusText
2011-01-05 22:41:23 +01:00
}
2011-01-05 22:41:23 +01:00
responseHeaders = xhr.getAllResponseHeaders();
2011-01-05 22:41:23 +01:00
// Filter status for non standard behaviours
// (so many they seem to be the actual "standard")
status =
// Opera returns 0 when it should be 304
// Webkit returns 0 for failing cross-domain no matter the real status
status === 0 ?
(
! s.crossDomain || statusText ? // Webkit, Firefox: filter out faulty cross-domain requests
(
responseHeaders ? // Opera: filter out real aborts #6060
304
:
0
)
:
302 // We assume 302 but could be anything cross-domain related
)
:
(
status == 1223 ? // IE sometimes returns 1223 when it should be 204 (see #1450)
204
:
status
);
2011-01-05 22:41:23 +01:00
// Guess response & update dataType accordingly
response =
determineDataType(
s,
xhr.getResponseHeader("content-type"),
xhr.responseText,
xhr.responseXML );
}
2011-01-05 22:41:23 +01:00
// Call complete
complete(status,statusText,response,responseHeaders);
}
};
2011-01-05 22:41:23 +01:00
// if we're in sync mode
// or it's in cache and has been retrieved directly (IE6 & IE7)
// we need to manually fire the callback
if ( ! s.async || xhr.readyState === 4 ) {
2011-01-05 22:41:23 +01:00
callback();
2011-01-05 22:41:23 +01:00
} else {
2011-01-05 22:41:23 +01:00
// Listener is externalized to handle abort on unload
handle = xhrPollingId++;
xhrs[ handle ] = xhr;
xhr.onreadystatechange = function() {
callback();
};
2011-01-05 22:41:23 +01:00
}
},
2011-01-05 22:41:23 +01:00
abort: function(statusText) {
if ( callback ) {
callback(statusText);
}
}
};
}
});
// #5280: we need to abort on unload or IE will keep connections alive
jQuery(window).bind( "unload" , function() {
2011-01-05 22:41:23 +01:00
// Abort all pending requests
jQuery.each(xhrs, function(_, xhr) {
if ( xhr.onreadystatechange ) {
xhr.onreadystatechange( xhrUnloadAbortMarker );
}
});
2011-01-05 22:41:23 +01:00
// Resest polling structure to be safe
xhrs = {};
2011-01-05 22:41:23 +01:00
});
})( jQuery );