b18274c437eff9488880ee2672960effe60ae5b4
[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 // 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
35          * we need a fallback.
36          */
37         function() {
38                 return !this.isLocal && createStandardXHR() || createActiveXHR();
39         } :
40         // For all other browsers, use the standard XMLHttpRequest object
41         createStandardXHR;
42
43 // Test if we can create an xhr object
44 testXHR = jQuery.ajaxSettings.xhr();
45 jQuery.support.ajax = !!testXHR;
46
47 // Does this browser support crossDomain XHR requests
48 jQuery.support.cors = testXHR && ( "withCredentials" in testXHR );
49
50 // No need for the temporary xhr anymore
51 testXHR = undefined;
52
53 // Create transport if the browser can provide an xhr
54 if ( jQuery.support.ajax ) {
55
56         jQuery.ajaxTransport(function( s ) {
57                 // Cross domain only allowed if supported through XMLHttpRequest
58                 if ( !s.crossDomain || jQuery.support.cors ) {
59
60                         var callback;
61
62                         return {
63                                 send: function( headers, complete ) {
64
65                                         // #5280: we need to abort on unload or IE will keep connections alive
66                                         if ( !xhrUnloadAbortInstalled ) {
67
68                                                 xhrUnloadAbortInstalled = 1;
69
70                                                 jQuery(window).bind( "unload", function() {
71
72                                                         // Abort all pending requests
73                                                         jQuery.each( xhrs, function( _, xhr ) {
74                                                                 if ( xhr.onreadystatechange ) {
75                                                                         xhr.onreadystatechange( 1 );
76                                                                 }
77                                                         } );
78
79                                                 } );
80                                         }
81
82                                         // Get a new xhr
83                                         var xhr = s.xhr(),
84                                                 handle,
85                                                 i;
86
87                                         // Open the socket
88                                         // Passing null username, generates a login popup on Opera (#2865)
89                                         if ( s.username ) {
90                                                 xhr.open( s.type, s.url, s.async, s.username, s.password );
91                                         } else {
92                                                 xhr.open( s.type, s.url, s.async );
93                                         }
94
95                                         // Apply custom fields if provided
96                                         if ( s.xhrFields ) {
97                                                 for ( i in s.xhrFields ) {
98                                                         xhr[ i ] = s.xhrFields[ i ];
99                                                 }
100                                         }
101
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";
108                                         }
109
110                                         // Need an extra try/catch for cross domain requests in Firefox 3
111                                         try {
112                                                 jQuery.each( headers, function( key, value ) {
113                                                         xhr.setRequestHeader( key, value );
114                                                 } );
115                                         } catch( _ ) {}
116
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 );
121
122                                         // Listener
123                                         callback = function( _, isAbort ) {
124
125                                                 var status,
126                                                         statusText,
127                                                         responseHeaders,
128                                                         responses,
129                                                         xml;
130
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)
134                                                 try {
135
136                                                         // Was never called and is aborted or complete
137                                                         if ( callback && ( isAbort || xhr.readyState === 4 ) ) {
138
139                                                                 // Only called once
140                                                                 callback = undefined;
141
142                                                                 // Do not keep as active anymore
143                                                                 if ( handle ) {
144                                                                         xhr.onreadystatechange = jQuery.noop;
145                                                                         delete xhrs[ handle ];
146                                                                 }
147
148                                                                 // If it's an abort
149                                                                 if ( isAbort ) {
150                                                                         // Abort it manually if needed
151                                                                         if ( xhr.readyState !== 4 ) {
152                                                                                 xhr.abort();
153                                                                         }
154                                                                 } else {
155                                                                         // Get info
156                                                                         status = xhr.status;
157                                                                         responseHeaders = xhr.getAllResponseHeaders();
158                                                                         responses = {};
159                                                                         xml = xhr.responseXML;
160
161                                                                         // Construct response list
162                                                                         if ( xml && xml.documentElement /* #4958 */ ) {
163                                                                                 responses.xml = xml;
164                                                                         }
165                                                                         responses.text = xhr.responseText;
166
167                                                                         // Firefox throws an exception when accessing
168                                                                         // statusText for faulty cross-domain requests
169                                                                         try {
170                                                                                 statusText = xhr.statusText;
171                                                                         } catch( e ) {
172                                                                                 // We normalize with Webkit giving an empty statusText
173                                                                                 statusText = "";
174                                                                         }
175
176                                                                         // Filter status for non standard behaviors
177
178                                                                         // IE - #1450: sometimes returns 1223 when it should be 204
179                                                                         if ( status === 1223 ) {
180                                                                                 status = 204;
181                                                                         // Various - #8177: a Not Modified response was received
182                                                                         // yet no conditional request headers was provided
183                                                                         } else if ( status === 304 &&
184                                                                                                 !headers[ "if-modified-since" ] &&
185                                                                                                 !headers[ "if-none-match" ] ) {
186                                                                                 status = 200;
187                                                                         // Status 0 encompasses several cases
188                                                                         } else if ( !status ) {
189                                                                                 // Cross-domain
190                                                                                 if ( s.crossDomain ) {
191                                                                                         if ( !s.statusText ) {
192                                                                                                 // FF, Webkit (other?): There is no status text for errors
193                                                                                                 // 302 is the most generic cross-domain status code
194                                                                                                 // for errors, could be anything really (even a real 0)
195                                                                                                 status = 302;
196                                                                                         }
197                                                                                 // All same-domain: for local files, 0 is a success
198                                                                                 } else if( s.isLocal ) {
199                                                                                         status = 200;
200                                                                                         // Opera: this notifies success for all requests
201                                                                                         // (verified in 11.01). Patch welcome.
202                                                                                 }
203                                                                                 // Opera - #6060: sets status as 0 for 304
204                                                                                 // Patch welcome.
205                                                                         }
206                                                                 }
207                                                         }
208                                                 } catch( firefoxAccessException ) {
209                                                         if ( !isAbort ) {
210                                                                 complete( -1, firefoxAccessException );
211                                                         }
212                                                 }
213
214                                                 // Call complete if needed
215                                                 if ( responses ) {
216                                                         complete( status, statusText, responses, responseHeaders );
217                                                 }
218                                         };
219
220                                         // if we're in sync mode or it's in cache
221                                         // and has been retrieved directly (IE6 & IE7)
222                                         // we need to manually fire the callback
223                                         if ( !s.async || xhr.readyState === 4 ) {
224                                                 callback();
225                                         } else {
226                                                 // Add to list of active xhrs
227                                                 handle = xhrId++;
228                                                 xhrs[ handle ] = xhr;
229                                                 xhr.onreadystatechange = callback;
230                                         }
231                                 },
232
233                                 abort: function() {
234                                         if ( callback ) {
235                                                 callback(0,1);
236                                         }
237                                 }
238                         };
239                 }
240         });
241 }
242
243 })( jQuery );