3 var // Next active xhr id
10 xhrUnloadAbortInstalled,
12 // XHR used to determine supports properties
15 // Create the request object
16 // (This is still attached to ajaxSettings for backward compatibility)
17 jQuery.ajaxSettings.xhr = window.ActiveXObject ?
18 /* Microsoft failed to properly
19 * implement the XMLHttpRequest in IE7 (can't request local files),
20 * so we use the ActiveXObject when it is available
21 * Additionally XMLHttpRequest can be disabled in IE7/IE8 so
25 if ( window.location.protocol !== "file:" ) {
27 return new window.XMLHttpRequest();
28 } catch( xhrError ) {}
32 return new window.ActiveXObject("Microsoft.XMLHTTP");
33 } catch( activeError ) {}
35 // For all other browsers, use the standard XMLHttpRequest object
37 return new window.XMLHttpRequest();
40 // Test if we can create an xhr object
42 testXHR = jQuery.ajaxSettings.xhr();
43 } catch( xhrCreationException ) {}
45 //Does this browser support XHR requests?
46 jQuery.support.ajax = !!testXHR;
48 // Does this browser support crossDomain XHR requests
49 jQuery.support.cors = testXHR && ( "withCredentials" in testXHR );
51 // No need for the temporary xhr anymore
54 // Create transport if the browser can provide an xhr
55 if ( jQuery.support.ajax ) {
57 jQuery.ajaxTransport(function( s ) {
58 // Cross domain only allowed if supported through XMLHttpRequest
59 if ( !s.crossDomain || jQuery.support.cors ) {
64 send: function( headers, complete ) {
66 // #5280: we need to abort on unload or IE will keep connections alive
67 if ( !xhrUnloadAbortInstalled ) {
69 xhrUnloadAbortInstalled = 1;
71 jQuery(window).bind( "unload", function() {
73 // Abort all pending requests
74 jQuery.each( xhrs, function( _, xhr ) {
75 if ( xhr.onreadystatechange ) {
76 xhr.onreadystatechange( 1 );
88 // Passing null username, generates a login popup on Opera (#2865)
90 xhr.open( s.type, s.url, s.async, s.username, s.password );
92 xhr.open( s.type, s.url, s.async );
95 // Requested-With header
96 // Not set for crossDomain requests with no content
97 // (see why at http://trac.dojotoolkit.org/ticket/9486)
98 // Won't change header if already provided
99 if ( !( s.crossDomain && !s.hasContent ) && !headers["x-requested-with"] ) {
100 headers[ "x-requested-with" ] = "XMLHttpRequest";
103 // Need an extra try/catch for cross domain requests in Firefox 3
105 jQuery.each( headers, function( key, value ) {
106 xhr.setRequestHeader( key, value );
110 // Do send the request
111 // This may raise an exception which is actually
112 // handled in jQuery.ajax (so no try/catch here)
113 xhr.send( ( s.hasContent && s.data ) || null );
116 callback = function( _, isAbort ) {
124 // Firefox throws exceptions when accessing properties
125 // of an xhr when a network error occured
126 // http://helpful.knobs-dials.com/index.php/Component_returned_failure_code:_0x80040111_(NS_ERROR_NOT_AVAILABLE)
129 // Was never called and is aborted or complete
130 if ( callback && ( isAbort || xhr.readyState === 4 ) ) {
133 callback = undefined;
135 // Do not keep as active anymore
137 xhr.onreadystatechange = jQuery.noop;
138 delete xhrs[ handle ];
143 // Abort it manually if needed
144 if ( xhr.readyState !== 4 ) {
150 responseHeaders = xhr.getAllResponseHeaders();
152 xml = xhr.responseXML;
154 // Construct response list
155 if ( xml && xml.documentElement /* #4958 */ ) {
158 responses.text = xhr.responseText;
160 // Firefox throws an exception when accessing
161 // statusText for faulty cross-domain requests
163 statusText = xhr.statusText;
165 // We normalize with Webkit giving an empty statusText
169 // Filter status for non standard behaviors
171 // Most browsers return 0 when it should be 200 for local files
172 // Opera returns 0 when it should be 304
173 // Webkit returns 0 for failing cross-domain no matter the real status
175 // All: for local files, 0 is a success
176 ( location.protocol === "file:" ? 200 : (
177 // Webkit, Firefox: filter out faulty cross-domain requests
178 !s.crossDomain || statusText ?
180 // Opera: filter out real aborts #6060
185 // We assume 302 but could be anything cross-domain related
189 // IE sometimes returns 1223 when it should be 204 (see #1450)
196 } catch( firefoxAccessException ) {
198 complete( -1, firefoxAccessException );
202 // Call complete if needed
204 complete( status, statusText, responses, responseHeaders );
208 // if we're in sync mode or it's in cache
209 // and has been retrieved directly (IE6 & IE7)
210 // we need to manually fire the callback
211 if ( !s.async || xhr.readyState === 4 ) {
214 // Add to list of active xhrs
216 xhrs[ handle ] = xhr;
217 xhr.onreadystatechange = callback;