3 var // Next fake timer id
4 xhrPollingId = jQuery.now(),
9 // #5280: see end of file
10 xhrUnloadAbortMarker = [];
13 jQuery.xhr.transport( function( s , determineDataType ) {
15 // Cross domain only allowed if supported through XMLHttpRequest
16 if ( ! s.crossDomain || jQuery.support.cors ) {
22 send: function(headers, complete) {
28 // Passing null username, generates a login popup on Opera (#2865)
30 xhr.open(s.type, s.url, s.async, s.username, s.password);
32 xhr.open(s.type, s.url, s.async);
35 // Requested-With header
36 // Not set for crossDomain requests with no content
37 // (see why at http://trac.dojotoolkit.org/ticket/9486)
38 // Won't change header if already provided in beforeSend
39 if ( ! ( s.crossDomain && ! s.hasContent ) && ! headers["x-requested-with"] ) {
40 headers["x-requested-with"] = "XMLHttpRequest";
43 // Need an extra try/catch for cross domain requests in Firefox 3
46 jQuery.each(headers, function(key,value) {
47 xhr.setRequestHeader(key,value);
52 // Do send the request
54 xhr.send( ( s.hasContent && s.data ) || null );
56 complete(0, "error", "" + e);
61 callback = function ( abortStatusText ) {
63 // Was never called and is aborted or complete
64 if ( callback && ( abortStatusText || xhr.readyState === 4 ) ) {
66 // Do not listen anymore
68 xhr.onreadystatechange = jQuery.noop;
69 delete xhrs[ handle ];
76 var status, statusText, response, responseHeaders;
78 if ( abortStatusText ) {
80 if ( xhr.readyState !== 4 ) {
84 // Stop here if unloadAbort
85 if ( abortStatusText === xhrUnloadAbortMarker ) {
90 statusText = abortStatusText;
96 try { // Firefox throws an exception when accessing statusText for faulty cross-domain requests
98 statusText = xhr.statusText;
102 statusText = ""; // We normalize with Webkit giving an empty statusText
106 responseHeaders = xhr.getAllResponseHeaders();
108 // Filter status for non standard behaviours
109 // (so many they seem to be the actual "standard")
111 // Opera returns 0 when it should be 304
112 // Webkit returns 0 for failing cross-domain no matter the real status
115 ! s.crossDomain || statusText ? // Webkit, Firefox: filter out faulty cross-domain requests
117 responseHeaders ? // Opera: filter out real aborts #6060
123 302 // We assume 302 but could be anything cross-domain related
127 status == 1223 ? // IE sometimes returns 1223 when it should be 204 (see #1450)
133 // Guess response if needed & update datatype accordingly
134 if ( status >= 200 && status < 300 ) {
138 xhr.getResponseHeader("content-type"),
145 complete(status,statusText,response,responseHeaders);
149 // if we're in sync mode
150 // or it's in cache and has been retrieved directly (IE6 & IE7)
151 // we need to manually fire the callback
152 if ( ! s.async || xhr.readyState === 4 ) {
158 // Listener is externalized to handle abort on unload
159 handle = xhrPollingId++;
160 xhrs[ handle ] = xhr;
161 xhr.onreadystatechange = function() {
167 abort: function(statusText) {
169 callback(statusText);
176 // #5280: we need to abort on unload or IE will keep connections alive
177 jQuery(window).bind( "unload" , function() {
179 // Abort all pending requests
180 jQuery.each(xhrs, function(_, xhr) {
181 if ( xhr.onreadystatechange ) {
182 xhr.onreadystatechange( xhrUnloadAbortMarker );
186 // Resest polling structure to be safe