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 // Keep track of isLocal in case it gets removed
29 // from ajaxSettings later on
30 protocolIsLocal = jQuery.ajaxSettings.isLocal;
32 // Create the request object
33 // (This is still attached to ajaxSettings for backward compatibility)
34 jQuery.ajaxSettings.xhr = window.ActiveXObject ?
35 /* Microsoft failed to properly
36 * implement the XMLHttpRequest in IE7 (can't request local files),
37 * so we use the ActiveXObject when it is available
38 * Additionally XMLHttpRequest can be disabled in IE7/IE8 so
44 return createStandardXHR() || createActiveXHR();
47 // For all other browsers, use the standard XMLHttpRequest object
50 // Test if we can create an xhr object
51 testXHR = jQuery.ajaxSettings.xhr();
52 jQuery.support.ajax = !!testXHR;
54 // Does this browser support crossDomain XHR requests
55 jQuery.support.cors = testXHR && ( "withCredentials" in testXHR );
57 // No need for the temporary xhr anymore
60 // Create transport if the browser can provide an xhr
61 if ( jQuery.support.ajax ) {
63 jQuery.ajaxTransport(function( s ) {
64 // Cross domain only allowed if supported through XMLHttpRequest
65 if ( !s.crossDomain || jQuery.support.cors ) {
70 send: function( headers, complete ) {
72 // #5280: we need to abort on unload or IE will keep connections alive
73 if ( !xhrUnloadAbortInstalled ) {
75 xhrUnloadAbortInstalled = 1;
77 jQuery(window).bind( "unload", function() {
79 // Abort all pending requests
80 jQuery.each( xhrs, function( _, xhr ) {
81 if ( xhr.onreadystatechange ) {
82 xhr.onreadystatechange( 1 );
95 // Passing null username, generates a login popup on Opera (#2865)
97 xhr.open( s.type, s.url, s.async, s.username, s.password );
99 xhr.open( s.type, s.url, s.async );
102 // Apply custom fields if provided
104 for ( i in s.xhrFields ) {
105 xhr[ i ] = s.xhrFields[ i ];
109 // Requested-With header
110 // Not set for crossDomain requests with no content
111 // (see why at http://trac.dojotoolkit.org/ticket/9486)
112 // Won't change header if already provided
113 if ( !( s.crossDomain && !s.hasContent ) && !headers["x-requested-with"] ) {
114 headers[ "x-requested-with" ] = "XMLHttpRequest";
117 // Need an extra try/catch for cross domain requests in Firefox 3
119 jQuery.each( headers, function( key, value ) {
120 xhr.setRequestHeader( key, value );
124 // Do send the request
125 // This may raise an exception which is actually
126 // handled in jQuery.ajax (so no try/catch here)
127 xhr.send( ( s.hasContent && s.data ) || null );
130 callback = function( _, isAbort ) {
138 // Firefox throws exceptions when accessing properties
139 // of an xhr when a network error occured
140 // http://helpful.knobs-dials.com/index.php/Component_returned_failure_code:_0x80040111_(NS_ERROR_NOT_AVAILABLE)
143 // Was never called and is aborted or complete
144 if ( callback && ( isAbort || xhr.readyState === 4 ) ) {
147 callback = undefined;
149 // Do not keep as active anymore
151 xhr.onreadystatechange = jQuery.noop;
152 delete xhrs[ handle ];
157 // Abort it manually if needed
158 if ( xhr.readyState !== 4 ) {
164 responseHeaders = xhr.getAllResponseHeaders();
166 xml = xhr.responseXML;
168 // Construct response list
169 if ( xml && xml.documentElement /* #4958 */ ) {
172 responses.text = xhr.responseText;
174 // Firefox throws an exception when accessing
175 // statusText for faulty cross-domain requests
177 statusText = xhr.statusText;
179 // We normalize with Webkit giving an empty statusText
183 // Filter status for non standard behaviors
185 // IE - #1450: sometimes returns 1223 when it should be 204
186 if ( status === 1223 ) {
188 // Status 0 encompasses several cases
189 } else if ( !status ) {
191 if ( s.crossDomain ) {
192 if ( !s.statusText ) {
193 // FF, Webkit (other?): There is no status text for errors
194 // 302 is the most generic cross-domain status code
195 // for errors, could be anything really (even a real 0)
198 // All same-domain: for local files, 0 is a success
199 } else if( protocolIsLocal ) {
201 // Opera: this notifies success for all requests
202 // (verified in 11.01). Patch welcome.
204 // Opera - #6060: sets status as 0 for 304
209 } catch( firefoxAccessException ) {
211 complete( -1, firefoxAccessException );
215 // Call complete if needed
217 complete( status, statusText, responses, responseHeaders );
221 // if we're in sync mode or it's in cache
222 // and has been retrieved directly (IE6 & IE7)
223 // we need to manually fire the callback
224 if ( !s.async || xhr.readyState === 4 ) {
227 // Add to list of active xhrs
229 xhrs[ handle ] = xhr;
230 xhr.onreadystatechange = callback;