3 var // Next fake timer id
4 xhrPollingId = jQuery.now(),
12 // #5280: see end of file
16 jQuery.ajax.transport( function( s , determineDataType ) {
18 // Cross domain only allowed if supported through XMLHttpRequest
19 if ( ! s.crossDomain || jQuery.support.cors ) {
25 send: function(headers, complete) {
27 // #5280: we need to abort on unload or IE will keep connections alive
28 if ( ! xhrUnloadAbortMarker ) {
30 xhrUnloadAbortMarker = [];
32 jQuery(window).bind( "unload" , function() {
34 // Abort all pending requests
35 jQuery.each(xhrs, function(_, xhr) {
36 if ( xhr.onreadystatechange ) {
37 xhr.onreadystatechange( xhrUnloadAbortMarker );
41 // Reset polling structure to be safe
47 var xhr = xhrPool.pop() || s.xhr(),
51 // Passing null username, generates a login popup on Opera (#2865)
53 xhr.open(s.type, s.url, s.async, s.username, s.password);
55 xhr.open(s.type, s.url, s.async);
58 // Requested-With header
59 // Not set for crossDomain requests with no content
60 // (see why at http://trac.dojotoolkit.org/ticket/9486)
61 // Won't change header if already provided in beforeSend
62 if ( ! ( s.crossDomain && ! s.hasContent ) && ! headers["x-requested-with"] ) {
63 headers["x-requested-with"] = "XMLHttpRequest";
66 // Need an extra try/catch for cross domain requests in Firefox 3
69 jQuery.each(headers, function(key,value) {
70 xhr.setRequestHeader(key,value);
75 // Do send the request
77 xhr.send( ( s.hasContent && s.data ) || null );
81 complete(0, "error", "" + e);
86 callback = function ( abortStatusText ) {
88 // Was never called and is aborted or complete
89 if ( callback && ( abortStatusText || xhr.readyState === 4 ) ) {
91 // Do not listen anymore
92 // and Store back in pool
94 xhr.onreadystatechange = jQuery.noop;
95 delete xhrs[ handle ];
103 var status, statusText, response, responseHeaders;
105 if ( abortStatusText ) {
107 if ( xhr.readyState !== 4 ) {
111 // Stop here if unloadAbort
112 if ( abortStatusText === xhrUnloadAbortMarker ) {
117 statusText = abortStatusText;
123 try { // Firefox throws an exception when accessing statusText for faulty cross-domain requests
125 statusText = xhr.statusText;
129 statusText = ""; // We normalize with Webkit giving an empty statusText
133 responseHeaders = xhr.getAllResponseHeaders();
135 // Filter status for non standard behaviours
136 // (so many they seem to be the actual "standard")
138 // Opera returns 0 when it should be 304
139 // Webkit returns 0 for failing cross-domain no matter the real status
142 ! s.crossDomain || statusText ? // Webkit, Firefox: filter out faulty cross-domain requests
144 responseHeaders ? // Opera: filter out real aborts #6060
150 302 // We assume 302 but could be anything cross-domain related
154 status == 1223 ? // IE sometimes returns 1223 when it should be 204 (see #1450)
160 // Guess response & update dataType accordingly
164 xhr.getResponseHeader("content-type"),
170 complete(status,statusText,response,responseHeaders);
174 // if we're in sync mode
175 // or it's in cache and has been retrieved directly (IE6 & IE7)
176 // we need to manually fire the callback
177 if ( ! s.async || xhr.readyState === 4 ) {
183 // Listener is externalized to handle abort on unload
184 handle = xhrPollingId++;
185 xhrs[ handle ] = xhr;
186 xhr.onreadystatechange = function() {
192 abort: function(statusText) {
194 callback(statusText);