Fixes #8152 by applying the same special cases for protocol "chrome-extension:" as...
[jquery.git] / src / ajax / xhr.js
1 (function( jQuery ) {
2
3 var // Next active xhr id
4         xhrId = jQuery.now(),
5
6         // active xhrs
7         xhrs = {},
8
9         // #5280: see below
10         xhrUnloadAbortInstalled,
11
12         // XHR used to determine supports properties
13         testXHR;
14
15 // Create the request object
16 // (This is still attached to ajaxSettings for backward compatibility)
17 jQuery.ajaxSettings.xhr = window.ActiveXObject ?
18         /* Microsoft failed to properly
19          * implement the XMLHttpRequest in IE7 (can't request local files),
20          * so we use the ActiveXObject when it is available
21          * Additionally XMLHttpRequest can be disabled in IE7/IE8 so
22          * we need a fallback.
23          */
24         function() {
25                 if ( !jQuery.ajaxSettings.isLocal ) {
26                         try {
27                                 return new window.XMLHttpRequest();
28                         } catch( xhrError ) {}
29                 }
30
31                 try {
32                         return new window.ActiveXObject("Microsoft.XMLHTTP");
33                 } catch( activeError ) {}
34         } :
35         // For all other browsers, use the standard XMLHttpRequest object
36         function() {
37                 return new window.XMLHttpRequest();
38         };
39
40 // Test if we can create an xhr object
41 try {
42         testXHR = jQuery.ajaxSettings.xhr();
43 } catch( xhrCreationException ) {}
44
45 //Does this browser support XHR requests?
46 jQuery.support.ajax = !!testXHR;
47
48 // Does this browser support crossDomain XHR requests
49 jQuery.support.cors = testXHR && ( "withCredentials" in testXHR );
50
51 // No need for the temporary xhr anymore
52 testXHR = undefined;
53
54 // Create transport if the browser can provide an xhr
55 if ( jQuery.support.ajax ) {
56
57         jQuery.ajaxTransport(function( s ) {
58                 // Cross domain only allowed if supported through XMLHttpRequest
59                 if ( !s.crossDomain || jQuery.support.cors ) {
60
61                         var callback;
62
63                         return {
64                                 send: function( headers, complete ) {
65
66                                         // #5280: we need to abort on unload or IE will keep connections alive
67                                         if ( !xhrUnloadAbortInstalled ) {
68
69                                                 xhrUnloadAbortInstalled = 1;
70
71                                                 jQuery(window).bind( "unload", function() {
72
73                                                         // Abort all pending requests
74                                                         jQuery.each( xhrs, function( _, xhr ) {
75                                                                 if ( xhr.onreadystatechange ) {
76                                                                         xhr.onreadystatechange( 1 );
77                                                                 }
78                                                         } );
79
80                                                 } );
81                                         }
82
83                                         // Get a new xhr
84                                         var xhr = s.xhr(),
85                                                 handle,
86                                                 i;
87
88                                         // Apply custom fields if provided
89                                         if ( s.xhrFields ) {
90                                                 for ( i in s.xhrFields ) {
91                                                         xhr[ i ] = s.xhrFields[ i ];
92                                                 }
93                                         }
94
95                                         // Open the socket
96                                         // Passing null username, generates a login popup on Opera (#2865)
97                                         if ( s.username ) {
98                                                 xhr.open( s.type, s.url, s.async, s.username, s.password );
99                                         } else {
100                                                 xhr.open( s.type, s.url, s.async );
101                                         }
102
103                                         // Requested-With header
104                                         // Not set for crossDomain requests with no content
105                                         // (see why at http://trac.dojotoolkit.org/ticket/9486)
106                                         // Won't change header if already provided
107                                         if ( !( s.crossDomain && !s.hasContent ) && !headers["x-requested-with"] ) {
108                                                 headers[ "x-requested-with" ] = "XMLHttpRequest";
109                                         }
110
111                                         // Need an extra try/catch for cross domain requests in Firefox 3
112                                         try {
113                                                 jQuery.each( headers, function( key, value ) {
114                                                         xhr.setRequestHeader( key, value );
115                                                 } );
116                                         } catch( _ ) {}
117
118                                         // Do send the request
119                                         // This may raise an exception which is actually
120                                         // handled in jQuery.ajax (so no try/catch here)
121                                         xhr.send( ( s.hasContent && s.data ) || null );
122
123                                         // Listener
124                                         callback = function( _, isAbort ) {
125
126                                                 var status,
127                                                         statusText,
128                                                         responseHeaders,
129                                                         responses,
130                                                         xml;
131
132                                                 // Firefox throws exceptions when accessing properties
133                                                 // of an xhr when a network error occured
134                                                 // http://helpful.knobs-dials.com/index.php/Component_returned_failure_code:_0x80040111_(NS_ERROR_NOT_AVAILABLE)
135                                                 try {
136
137                                                         // Was never called and is aborted or complete
138                                                         if ( callback && ( isAbort || xhr.readyState === 4 ) ) {
139
140                                                                 // Only called once
141                                                                 callback = undefined;
142
143                                                                 // Do not keep as active anymore
144                                                                 if ( handle ) {
145                                                                         xhr.onreadystatechange = jQuery.noop;
146                                                                         delete xhrs[ handle ];
147                                                                 }
148
149                                                                 // If it's an abort
150                                                                 if ( isAbort ) {
151                                                                         // Abort it manually if needed
152                                                                         if ( xhr.readyState !== 4 ) {
153                                                                                 xhr.abort();
154                                                                         }
155                                                                 } else {
156                                                                         // Get info
157                                                                         status = xhr.status;
158                                                                         responseHeaders = xhr.getAllResponseHeaders();
159                                                                         responses = {};
160                                                                         xml = xhr.responseXML;
161
162                                                                         // Construct response list
163                                                                         if ( xml && xml.documentElement /* #4958 */ ) {
164                                                                                 responses.xml = xml;
165                                                                         }
166                                                                         responses.text = xhr.responseText;
167
168                                                                         // Firefox throws an exception when accessing
169                                                                         // statusText for faulty cross-domain requests
170                                                                         try {
171                                                                                 statusText = xhr.statusText;
172                                                                         } catch( e ) {
173                                                                                 // We normalize with Webkit giving an empty statusText
174                                                                                 statusText = "";
175                                                                         }
176
177                                                                         // Filter status for non standard behaviors
178
179                                                                         // IE - #1450: sometimes returns 1223 when it should be 204
180                                                                         if ( status === 1223 ) {
181                                                                                 status = 204;
182                                                                         // Status 0 encompasses several cases
183                                                                         } else if ( !status ) {
184                                                                                 // Cross-domain
185                                                                                 if ( s.crossDomain ) {
186                                                                                         if ( !s.statusText ) {
187                                                                                                 // FF, Webkit (other?): There is no status text for errors
188                                                                                                 // 302 is the most generic cross-domain status code
189                                                                                                 // for errors, could be anything really (even a real 0)
190                                                                                                 status = 302;
191                                                                                         }
192                                                                                 // All same-domain - #8125, #8152: for local files, 0 is a success
193                                                                                 } else if( s.isLocal ) {
194                                                                                         status = 200;
195                                                                                 }
196                                                                                 // Opera - #6060: sets status as 0 for 304
197                                                                                 // and there doesn't seem to be any way to
198                                                                                 // detect this case. Patch VERY welcome.
199                                                                         }
200                                                                 }
201                                                         }
202                                                 } catch( firefoxAccessException ) {
203                                                         if ( !isAbort ) {
204                                                                 complete( -1, firefoxAccessException );
205                                                         }
206                                                 }
207
208                                                 // Call complete if needed
209                                                 if ( responses ) {
210                                                         complete( status, statusText, responses, responseHeaders );
211                                                 }
212                                         };
213
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 ) {
218                                                 callback();
219                                         } else {
220                                                 // Add to list of active xhrs
221                                                 handle = xhrId++;
222                                                 xhrs[ handle ] = xhr;
223                                                 xhr.onreadystatechange = callback;
224                                         }
225                                 },
226
227                                 abort: function() {
228                                         if ( callback ) {
229                                                 callback(0,1);
230                                         }
231                                 }
232                         };
233                 }
234         });
235 }
236
237 })( jQuery );