3 // Functions to create xhrs
4 function createStandardXHR() {
6 return new window.XMLHttpRequest();
10 function createActiveXHR() {
12 return new window.ActiveXObject("Microsoft.XMLHTTP");
16 var // Next active xhr id
23 xhrUnloadAbortInstalled,
25 // XHR used to determine supports properties
28 // Create the request object
29 // (This is still attached to ajaxSettings for backward compatibility)
30 jQuery.ajaxSettings.xhr = window.ActiveXObject ?
31 /* Microsoft failed to properly
32 * implement the XMLHttpRequest in IE7 (can't request local files),
33 * so we use the ActiveXObject when it is available
34 * Additionally XMLHttpRequest can be disabled in IE7/IE8 so
38 return !this.isLocal && createStandardXHR() || createActiveXHR();
40 // For all other browsers, use the standard XMLHttpRequest object
43 // Test if we can create an xhr object
44 testXHR = jQuery.ajaxSettings.xhr();
45 jQuery.support.ajax = !!testXHR;
47 // Does this browser support crossDomain XHR requests
48 jQuery.support.cors = testXHR && ( "withCredentials" in testXHR );
50 // No need for the temporary xhr anymore
53 // Create transport if the browser can provide an xhr
54 if ( jQuery.support.ajax ) {
56 jQuery.ajaxTransport(function( s ) {
57 // Cross domain only allowed if supported through XMLHttpRequest
58 if ( !s.crossDomain || jQuery.support.cors ) {
63 send: function( headers, complete ) {
65 // #5280: we need to abort on unload or IE will keep connections alive
66 if ( !xhrUnloadAbortInstalled ) {
68 xhrUnloadAbortInstalled = 1;
70 jQuery(window).bind( "unload", function() {
72 // Abort all pending requests
73 jQuery.each( xhrs, function( _, xhr ) {
74 if ( xhr.onreadystatechange ) {
75 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 // Apply custom fields if provided
97 for ( i in s.xhrFields ) {
98 xhr[ i ] = s.xhrFields[ i ];
102 // Requested-With header
103 // Not set for crossDomain requests with no content
104 // (see why at http://trac.dojotoolkit.org/ticket/9486)
105 // Won't change header if already provided
106 if ( !( s.crossDomain && !s.hasContent ) && !headers["x-requested-with"] ) {
107 headers[ "x-requested-with" ] = "XMLHttpRequest";
110 // Need an extra try/catch for cross domain requests in Firefox 3
112 jQuery.each( headers, function( key, value ) {
113 xhr.setRequestHeader( key, value );
117 // Do send the request
118 // This may raise an exception which is actually
119 // handled in jQuery.ajax (so no try/catch here)
120 xhr.send( ( s.hasContent && s.data ) || null );
123 callback = function( _, isAbort ) {
131 // Firefox throws exceptions when accessing properties
132 // of an xhr when a network error occured
133 // http://helpful.knobs-dials.com/index.php/Component_returned_failure_code:_0x80040111_(NS_ERROR_NOT_AVAILABLE)
136 // Was never called and is aborted or complete
137 if ( callback && ( isAbort || xhr.readyState === 4 ) ) {
140 callback = undefined;
142 // Do not keep as active anymore
144 xhr.onreadystatechange = jQuery.noop;
145 delete xhrs[ handle ];
150 // Abort it manually if needed
151 if ( xhr.readyState !== 4 ) {
157 responseHeaders = xhr.getAllResponseHeaders();
159 xml = xhr.responseXML;
161 // Construct response list
162 if ( xml && xml.documentElement /* #4958 */ ) {
165 responses.text = xhr.responseText;
167 // Firefox throws an exception when accessing
168 // statusText for faulty cross-domain requests
170 statusText = xhr.statusText;
172 // We normalize with Webkit giving an empty statusText
176 // Filter status for non standard behaviors
178 // IE - #1450: sometimes returns 1223 when it should be 204
179 if ( status === 1223 ) {
181 // Status 0 encompasses several cases
182 } else if ( !status ) {
184 if ( s.crossDomain ) {
185 if ( !s.statusText ) {
186 // FF, Webkit (other?): There is no status text for errors
187 // 302 is the most generic cross-domain status code
188 // for errors, could be anything really (even a real 0)
191 // All same-domain: for local files, 0 is a success
192 } else if( s.isLocal ) {
194 // Opera: this notifies success for all requests
195 // (verified in 11.01). Patch welcome.
197 // Opera - #6060: sets status as 0 for 304
202 } catch( firefoxAccessException ) {
204 complete( -1, firefoxAccessException );
208 // Call complete if needed
210 complete( status, statusText, responses, responseHeaders );
214 // if we're in sync mode or it's in cache
215 // and has been retrieved directly (IE6 & IE7)
216 // we need to manually fire the callback
217 if ( !s.async || xhr.readyState === 4 ) {
220 // Add to list of active xhrs
222 xhrs[ handle ] = xhr;
223 xhr.onreadystatechange = callback;