3 var // Next active xhr id
10 xhrUnloadAbortInstalled;
13 jQuery.ajaxTransport( function( s , determineDataType ) {
15 // Cross domain only allowed if supported through XMLHttpRequest
16 if ( ! s.crossDomain || jQuery.support.cors ) {
22 send: function(headers, complete) {
24 // #5280: we need to abort on unload or IE will keep connections alive
25 if ( ! xhrUnloadAbortInstalled ) {
27 xhrUnloadAbortInstalled = 1;
29 jQuery(window).bind( "unload" , function() {
31 // Abort all pending requests
32 jQuery.each(xhrs, function(_, xhr) {
33 if ( xhr.onreadystatechange ) {
34 xhr.onreadystatechange( 1 );
46 // Passing null username, generates a login popup on Opera (#2865)
48 xhr.open(s.type, s.url, s.async, s.username, s.password);
50 xhr.open(s.type, s.url, s.async);
53 // Requested-With header
54 // Not set for crossDomain requests with no content
55 // (see why at http://trac.dojotoolkit.org/ticket/9486)
56 // Won't change header if already provided
57 if ( ! ( s.crossDomain && ! s.hasContent ) && ! headers["x-requested-with"] ) {
58 headers["x-requested-with"] = "XMLHttpRequest";
61 // Need an extra try/catch for cross domain requests in Firefox 3
64 jQuery.each(headers, function(key,value) {
65 xhr.setRequestHeader(key,value);
70 // Do send the request
72 xhr.send( ( s.hasContent && s.data ) || null );
74 complete(0, "error", "" + e);
79 callback = function( _ , isAbort ) {
81 // Was never called and is aborted or complete
82 if ( callback && ( isAbort || xhr.readyState === 4 ) ) {
87 // Do not keep as active anymore
88 // and store back into pool
90 xhr.onreadystatechange = jQuery.noop;
91 delete xhrs[ handle ];
97 // Abort it manually if needed
98 if ( xhr.readyState !== 4 ) {
104 var status = xhr.status,
107 responseHeaders = xhr.getAllResponseHeaders();
109 try { // Firefox throws an exception when accessing statusText for faulty cross-domain requests
111 statusText = xhr.statusText;
115 statusText = ""; // We normalize with Webkit giving an empty statusText
119 // Filter status for non standard behaviours
120 // (so many they seem to be the actual "standard")
122 // Opera returns 0 when it should be 304
123 // Webkit returns 0 for failing cross-domain no matter the real status
126 ! s.crossDomain || statusText ? // Webkit, Firefox: filter out faulty cross-domain requests
128 responseHeaders ? // Opera: filter out real aborts #6060
134 302 // We assume 302 but could be anything cross-domain related
138 status == 1223 ? // IE sometimes returns 1223 when it should be 204 (see #1450)
144 // Guess response & update dataType accordingly
148 xhr.getResponseHeader("content-type"),
153 complete(status,statusText,response,responseHeaders);
158 // if we're in sync mode
159 // or it's in cache and has been retrieved directly (IE6 & IE7)
160 // we need to manually fire the callback
161 if ( ! s.async || xhr.readyState === 4 ) {
167 // Add to list of active xhrs
169 xhrs[ handle ] = xhr;
170 xhr.onreadystatechange = callback;