Stores jQuery.ajaxSettings.isLocal locally at load time so that any change to it...
[jquery.git] / src / ajax / xhr.js
1 (function( jQuery ) {
2
3 // Functions to create xhrs
4 function createStandardXHR() {
5         try {
6                 return new window.XMLHttpRequest();
7         } catch( e ) {}
8 }
9
10 function createActiveXHR() {
11         try {
12                 return new window.ActiveXObject("Microsoft.XMLHTTP");
13         } catch( e ) {}
14 }
15
16 var // Next active xhr id
17         xhrId = jQuery.now(),
18
19         // active xhrs
20         xhrs = {},
21
22         // #5280: see below
23         xhrUnloadAbortInstalled,
24
25         // XHR used to determine supports properties
26         testXHR,
27
28         // Keep track of isLocal in case it gets removed
29         // from ajaxSettings later on
30         protocolIsLocal = jQuery.ajaxSettings.isLocal;
31
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
39          * we need a fallback.
40          */
41         ( protocolIsLocal ?
42                 createActiveXHR :
43                 function() {
44                         return createStandardXHR() || createActiveXHR();
45                 }
46         ) :
47         // For all other browsers, use the standard XMLHttpRequest object
48         createStandardXHR;
49
50 // Test if we can create an xhr object
51 testXHR = jQuery.ajaxSettings.xhr();
52 jQuery.support.ajax = !!testXHR;
53
54 // Does this browser support crossDomain XHR requests
55 jQuery.support.cors = testXHR && ( "withCredentials" in testXHR );
56
57 // No need for the temporary xhr anymore
58 testXHR = undefined;
59
60 // Create transport if the browser can provide an xhr
61 if ( jQuery.support.ajax ) {
62
63         jQuery.ajaxTransport(function( s ) {
64                 // Cross domain only allowed if supported through XMLHttpRequest
65                 if ( !s.crossDomain || jQuery.support.cors ) {
66
67                         var callback;
68
69                         return {
70                                 send: function( headers, complete ) {
71
72                                         // #5280: we need to abort on unload or IE will keep connections alive
73                                         if ( !xhrUnloadAbortInstalled ) {
74
75                                                 xhrUnloadAbortInstalled = 1;
76
77                                                 jQuery(window).bind( "unload", function() {
78
79                                                         // Abort all pending requests
80                                                         jQuery.each( xhrs, function( _, xhr ) {
81                                                                 if ( xhr.onreadystatechange ) {
82                                                                         xhr.onreadystatechange( 1 );
83                                                                 }
84                                                         } );
85
86                                                 } );
87                                         }
88
89                                         // Get a new xhr
90                                         var xhr = s.xhr(),
91                                                 handle,
92                                                 i;
93
94                                         // Open the socket
95                                         // Passing null username, generates a login popup on Opera (#2865)
96                                         if ( s.username ) {
97                                                 xhr.open( s.type, s.url, s.async, s.username, s.password );
98                                         } else {
99                                                 xhr.open( s.type, s.url, s.async );
100                                         }
101
102                                         // Apply custom fields if provided
103                                         if ( s.xhrFields ) {
104                                                 for ( i in s.xhrFields ) {
105                                                         xhr[ i ] = s.xhrFields[ i ];
106                                                 }
107                                         }
108
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";
115                                         }
116
117                                         // Need an extra try/catch for cross domain requests in Firefox 3
118                                         try {
119                                                 jQuery.each( headers, function( key, value ) {
120                                                         xhr.setRequestHeader( key, value );
121                                                 } );
122                                         } catch( _ ) {}
123
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 );
128
129                                         // Listener
130                                         callback = function( _, isAbort ) {
131
132                                                 var status,
133                                                         statusText,
134                                                         responseHeaders,
135                                                         responses,
136                                                         xml;
137
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)
141                                                 try {
142
143                                                         // Was never called and is aborted or complete
144                                                         if ( callback && ( isAbort || xhr.readyState === 4 ) ) {
145
146                                                                 // Only called once
147                                                                 callback = undefined;
148
149                                                                 // Do not keep as active anymore
150                                                                 if ( handle ) {
151                                                                         xhr.onreadystatechange = jQuery.noop;
152                                                                         delete xhrs[ handle ];
153                                                                 }
154
155                                                                 // If it's an abort
156                                                                 if ( isAbort ) {
157                                                                         // Abort it manually if needed
158                                                                         if ( xhr.readyState !== 4 ) {
159                                                                                 xhr.abort();
160                                                                         }
161                                                                 } else {
162                                                                         // Get info
163                                                                         status = xhr.status;
164                                                                         responseHeaders = xhr.getAllResponseHeaders();
165                                                                         responses = {};
166                                                                         xml = xhr.responseXML;
167
168                                                                         // Construct response list
169                                                                         if ( xml && xml.documentElement /* #4958 */ ) {
170                                                                                 responses.xml = xml;
171                                                                         }
172                                                                         responses.text = xhr.responseText;
173
174                                                                         // Firefox throws an exception when accessing
175                                                                         // statusText for faulty cross-domain requests
176                                                                         try {
177                                                                                 statusText = xhr.statusText;
178                                                                         } catch( e ) {
179                                                                                 // We normalize with Webkit giving an empty statusText
180                                                                                 statusText = "";
181                                                                         }
182
183                                                                         // Filter status for non standard behaviors
184
185                                                                         // IE - #1450: sometimes returns 1223 when it should be 204
186                                                                         if ( status === 1223 ) {
187                                                                                 status = 204;
188                                                                         // Status 0 encompasses several cases
189                                                                         } else if ( !status ) {
190                                                                                 // Cross-domain
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)
196                                                                                                 status = 302;
197                                                                                         }
198                                                                                 // All same-domain: for local files, 0 is a success
199                                                                                 } else if( protocolIsLocal ) {
200                                                                                         status = 200;
201                                                                                         // Opera: this notifies success for all requests
202                                                                                         // (verified in 11.01). Patch welcome.
203                                                                                 }
204                                                                                 // Opera - #6060: sets status as 0 for 304
205                                                                                 // Patch welcome.
206                                                                         }
207                                                                 }
208                                                         }
209                                                 } catch( firefoxAccessException ) {
210                                                         if ( !isAbort ) {
211                                                                 complete( -1, firefoxAccessException );
212                                                         }
213                                                 }
214
215                                                 // Call complete if needed
216                                                 if ( responses ) {
217                                                         complete( status, statusText, responses, responseHeaders );
218                                                 }
219                                         };
220
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 ) {
225                                                 callback();
226                                         } else {
227                                                 // Add to list of active xhrs
228                                                 handle = xhrId++;
229                                                 xhrs[ handle ] = xhr;
230                                                 xhr.onreadystatechange = callback;
231                                         }
232                                 },
233
234                                 abort: function() {
235                                         if ( callback ) {
236                                                 callback(0,1);
237                                         }
238                                 }
239                         };
240                 }
241         });
242 }
243
244 })( jQuery );