3 var // Next fake timer id
4 xhrPollingId = jQuery.now(),
12 // #5280: see end of file
13 xhrUnloadAbortMarker = [];
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 var xhr = xhrPool.pop() || s.xhr(),
31 // Passing null username, generates a login popup on Opera (#2865)
33 xhr.open(s.type, s.url, s.async, s.username, s.password);
35 xhr.open(s.type, s.url, s.async);
38 // Requested-With header
39 // Not set for crossDomain requests with no content
40 // (see why at http://trac.dojotoolkit.org/ticket/9486)
41 // Won't change header if already provided in beforeSend
42 if ( ! ( s.crossDomain && ! s.hasContent ) && ! headers["x-requested-with"] ) {
43 headers["x-requested-with"] = "XMLHttpRequest";
46 // Need an extra try/catch for cross domain requests in Firefox 3
49 jQuery.each(headers, function(key,value) {
50 xhr.setRequestHeader(key,value);
55 // Do send the request
57 xhr.send( ( s.hasContent && s.data ) || null );
61 complete(0, "error", "" + e);
66 callback = function ( abortStatusText ) {
68 // Was never called and is aborted or complete
69 if ( callback && ( abortStatusText || xhr.readyState === 4 ) ) {
71 // Do not listen anymore
72 // and Store back in pool
74 xhr.onreadystatechange = jQuery.noop;
75 delete xhrs[ handle ];
83 var status, statusText, response, responseHeaders;
85 if ( abortStatusText ) {
87 if ( xhr.readyState !== 4 ) {
91 // Stop here if unloadAbort
92 if ( abortStatusText === xhrUnloadAbortMarker ) {
97 statusText = abortStatusText;
103 try { // Firefox throws an exception when accessing statusText for faulty cross-domain requests
105 statusText = xhr.statusText;
109 statusText = ""; // We normalize with Webkit giving an empty statusText
113 responseHeaders = xhr.getAllResponseHeaders();
115 // Filter status for non standard behaviours
116 // (so many they seem to be the actual "standard")
118 // Opera returns 0 when it should be 304
119 // Webkit returns 0 for failing cross-domain no matter the real status
122 ! s.crossDomain || statusText ? // Webkit, Firefox: filter out faulty cross-domain requests
124 responseHeaders ? // Opera: filter out real aborts #6060
130 302 // We assume 302 but could be anything cross-domain related
134 status == 1223 ? // IE sometimes returns 1223 when it should be 204 (see #1450)
140 // Guess response if needed & update datatype accordingly
141 if ( status >= 200 && status < 300 ) {
145 xhr.getResponseHeader("content-type"),
152 complete(status,statusText,response,responseHeaders);
156 // if we're in sync mode
157 // or it's in cache and has been retrieved directly (IE6 & IE7)
158 // we need to manually fire the callback
159 if ( ! s.async || xhr.readyState === 4 ) {
165 // Listener is externalized to handle abort on unload
166 handle = xhrPollingId++;
167 xhrs[ handle ] = xhr;
168 xhr.onreadystatechange = function() {
174 abort: function(statusText) {
176 callback(statusText);
183 // #5280: we need to abort on unload or IE will keep connections alive
184 jQuery(window).bind( "unload" , function() {
186 // Abort all pending requests
187 jQuery.each(xhrs, function(_, xhr) {
188 if ( xhr.onreadystatechange ) {
189 xhr.onreadystatechange( xhrUnloadAbortMarker );
193 // Resest polling structure to be safe