3 var // #5280: next active xhr id and list of active xhrs' callbacks
7 // XHR used to determine supports properties
10 // #5280: Internet Explorer will keep connections alive if we don't abort on unload
11 function xhrOnUnloadAbort() {
12 jQuery( window ).unload(function() {
13 // Abort all pending requests
14 for ( var key in xhrCallbacks ) {
15 xhrCallbacks[ key ]( 0, 1 );
20 // Functions to create xhrs
21 function createStandardXHR() {
23 return new window.XMLHttpRequest();
27 function createActiveXHR() {
29 return new window.ActiveXObject("Microsoft.XMLHTTP");
33 // Create the request object
34 // (This is still attached to ajaxSettings for backward compatibility)
35 jQuery.ajaxSettings.xhr = window.ActiveXObject ?
36 /* Microsoft failed to properly
37 * implement the XMLHttpRequest in IE7 (can't request local files),
38 * so we use the ActiveXObject when it is available
39 * Additionally XMLHttpRequest can be disabled in IE7/IE8 so
43 return !this.isLocal && createStandardXHR() || createActiveXHR();
45 // For all other browsers, use the standard XMLHttpRequest object
48 // Test if we can create an xhr object
49 testXHR = jQuery.ajaxSettings.xhr();
50 jQuery.support.ajax = !!testXHR;
52 // Does this browser support crossDomain XHR requests
53 jQuery.support.cors = testXHR && ( "withCredentials" in testXHR );
55 // No need for the temporary xhr anymore
58 // Create transport if the browser can provide an xhr
59 if ( jQuery.support.ajax ) {
61 jQuery.ajaxTransport(function( s ) {
62 // Cross domain only allowed if supported through XMLHttpRequest
63 if ( !s.crossDomain || jQuery.support.cors ) {
68 send: function( headers, complete ) {
76 // Passing null username, generates a login popup on Opera (#2865)
78 xhr.open( s.type, s.url, s.async, s.username, s.password );
80 xhr.open( s.type, s.url, s.async );
83 // Apply custom fields if provided
85 for ( i in s.xhrFields ) {
86 xhr[ i ] = s.xhrFields[ i ];
90 // Override mime type if needed
91 if ( s.mimeType && xhr.overrideMimeType ) {
92 xhr.overrideMimeType( s.mimeType );
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 xhrCallbacks[ handle ];
143 // Abort it manually if needed
144 if ( xhr.readyState !== 4 ) {
149 responseHeaders = xhr.getAllResponseHeaders();
151 xml = xhr.responseXML;
153 // Construct response list
154 if ( xml && xml.documentElement /* #4958 */ ) {
157 responses.text = xhr.responseText;
159 // Firefox throws an exception when accessing
160 // statusText for faulty cross-domain requests
162 statusText = xhr.statusText;
164 // We normalize with Webkit giving an empty statusText
168 // Filter status for non standard behaviors
170 // IE - #1450: sometimes returns 1223 when it should be 204
171 if ( status === 1223 ) {
173 // Various - #8177: a Not Modified response was received
174 // yet no conditional request headers was provided
175 } else if ( status === 304 &&
176 !headers[ "if-modified-since" ] &&
177 !headers[ "if-none-match" ] ) {
179 // Status 0 encompasses several cases
180 } else if ( !status ) {
182 if ( s.crossDomain ) {
183 if ( !s.statusText ) {
184 // FF, Webkit (other?): There is no status text for errors
185 // 302 is the most generic cross-domain status code
186 // for errors, could be anything really (even a real 0)
189 // All same-domain: for local files, 0 is a success
190 } else if( s.isLocal ) {
192 // Opera: this notifies success for all requests
193 // (verified in 11.01). Patch welcome.
195 // Opera - #6060: sets status as 0 for 304
200 } catch( firefoxAccessException ) {
202 complete( -1, firefoxAccessException );
206 // Call complete if needed
208 complete( status, statusText, responses, responseHeaders );
212 // if we're in sync mode or it's in cache
213 // and has been retrieved directly (IE6 & IE7)
214 // we need to manually fire the callback
215 if ( !s.async || xhr.readyState === 4 ) {
218 // Create the active xhrs callbacks list if needed
219 // and attach the unload handler
220 if ( !xhrCallbacks ) {
224 // Add to list of active xhrs callbacks
226 xhr.onreadystatechange = xhrCallbacks[ handle ] = callback;