Moves determineResponse logic into main ajax callback. Puts responseXXX fields defini...
[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 ( window.location.protocol !== "file:" ) {
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         jQuery.ajaxTransport( function( s ) {
57
58                 // Cross domain only allowed if supported through XMLHttpRequest
59                 if ( ! s.crossDomain || jQuery.support.cors ) {
60
61                         var callback;
62
63                         return {
64
65                                 send: function(headers, complete) {
66
67                                         // #5280: we need to abort on unload or IE will keep connections alive
68                                         if ( ! xhrUnloadAbortInstalled ) {
69
70                                                 xhrUnloadAbortInstalled = 1;
71
72                                                 jQuery(window).bind( "unload" , function() {
73
74                                                         // Abort all pending requests
75                                                         jQuery.each(xhrs, function(_, xhr) {
76                                                                 if ( xhr.onreadystatechange ) {
77                                                                         xhr.onreadystatechange( 1 );
78                                                                 }
79                                                         });
80
81                                                 });
82                                         }
83
84                                         // Get a new xhr
85                                         var xhr = s.xhr(),
86                                                 handle;
87
88                                         // Open the socket
89                                         // Passing null username, generates a login popup on Opera (#2865)
90                                         if ( s.username ) {
91                                                 xhr.open(s.type, s.url, s.async, s.username, s.password);
92                                         } else {
93                                                 xhr.open(s.type, s.url, s.async);
94                                         }
95
96                                         // Requested-With header
97                                         // Not set for crossDomain requests with no content
98                                         // (see why at http://trac.dojotoolkit.org/ticket/9486)
99                                         // Won't change header if already provided
100                                         if ( ! ( s.crossDomain && ! s.hasContent ) && ! headers["x-requested-with"] ) {
101                                                 headers["x-requested-with"] = "XMLHttpRequest";
102                                         }
103
104                                         // Need an extra try/catch for cross domain requests in Firefox 3
105                                         try {
106
107                                                 jQuery.each(headers, function(key,value) {
108                                                         xhr.setRequestHeader(key,value);
109                                                 });
110
111                                         } catch(_) {}
112
113                                         // Do send the request
114                                         try {
115                                                 xhr.send( ( s.hasContent && s.data ) || null );
116                                         } catch(e) {
117                                                 complete(0, "error", "" + e);
118                                                 return;
119                                         }
120
121                                         // Listener
122                                         callback = function( _ , isAbort ) {
123
124                                                 // Was never called and is aborted or complete
125                                                 if ( callback && ( isAbort || xhr.readyState === 4 ) ) {
126
127                                                         // Only called once
128                                                         callback = 0;
129
130                                                         // Do not keep as active anymore
131                                                         if (handle) {
132                                                                 xhr.onreadystatechange = jQuery.noop;
133                                                                 delete xhrs[ handle ];
134                                                         }
135
136                                                         // If it's an abort
137                                                         if ( isAbort ) {
138
139                                                                 // Abort it manually if needed
140                                                                 if ( xhr.readyState !== 4 ) {
141                                                                         xhr.abort();
142                                                                 }
143                                                         } else {
144
145                                                                 // Get info
146                                                                 var status = xhr.status,
147                                                                         statusText,
148                                                                         responseHeaders = xhr.getAllResponseHeaders(),
149                                                                         responses = {},
150                                                                         xml = xhr.responseXML;
151
152                                                                 // Construct response list
153                                                                 if ( xml && xml.documentElement /* #4958 */ ) {
154                                                                         responses.xml = xml;
155                                                                 }
156                                                                 responses.text = xhr.responseText;
157
158                                                                 try { // Firefox throws an exception when accessing statusText for faulty cross-domain requests
159
160                                                                         statusText = xhr.statusText;
161
162                                                                 } catch( e ) {
163
164                                                                         statusText = ""; // We normalize with Webkit giving an empty statusText
165
166                                                                 }
167
168                                                                 // Filter status for non standard behaviours
169                                                                 // (so many they seem to be the actual "standard")
170                                                                 status =
171                                                                         // Opera returns 0 when it should be 304
172                                                                         // Webkit returns 0 for failing cross-domain no matter the real status
173                                                                         status === 0 ?
174                                                                                 (
175                                                                                         ! s.crossDomain || statusText ? // Webkit, Firefox: filter out faulty cross-domain requests
176                                                                                         (
177                                                                                                 responseHeaders ? // Opera: filter out real aborts #6060
178                                                                                                 304
179                                                                                                 :
180                                                                                                 0
181                                                                                         )
182                                                                                         :
183                                                                                         302 // We assume 302 but could be anything cross-domain related
184                                                                                 )
185                                                                                 :
186                                                                                 (
187                                                                                         status == 1223 ?        // IE sometimes returns 1223 when it should be 204 (see #1450)
188                                                                                                 204
189                                                                                                 :
190                                                                                                 status
191                                                                                 );
192
193                                                                 // Call complete
194                                                                 complete(status,statusText,responses,responseHeaders);
195                                                         }
196                                                 }
197                                         };
198
199                                         // if we're in sync mode
200                                         // or it's in cache and has been retrieved directly (IE6 & IE7)
201                                         // we need to manually fire the callback
202                                         if ( ! s.async || xhr.readyState === 4 ) {
203
204                                                 callback();
205
206                                         } else {
207
208                                                 // Add to list of active xhrs
209                                                 handle = xhrId++;
210                                                 xhrs[ handle ] = xhr;
211                                                 xhr.onreadystatechange = callback;
212                                         }
213                                 },
214
215                                 abort: function() {
216                                         if ( callback ) {
217                                                 callback(0,1);
218                                         }
219                                 }
220                         };
221                 }
222         });
223 }
224
225 })( jQuery );