Fixes #7881. Setting contentType to false will prevent the Content-Type header from...
[jquery.git] / src / ajax / xhr.js
1 (function( jQuery ) {
2
3 var // Next fake timer id
4         xhrPollingId = jQuery.now(),
5
6         // Callbacks hashtable
7         xhrs = {},
8
9         // XHR pool
10         xhrPool = [],
11
12         // #5280: see end of file
13         xhrUnloadAbortMarker = [];
14
15
16 jQuery.ajax.transport( function( s , determineDataType ) {
17
18         // Cross domain only allowed if supported through XMLHttpRequest
19         if ( ! s.crossDomain || jQuery.support.cors ) {
20
21                 var callback;
22
23                 return {
24
25                         send: function(headers, complete) {
26
27                                 var xhr = xhrPool.pop() || s.xhr(),
28                                         handle;
29
30                                 // Open the socket
31                                 // Passing null username, generates a login popup on Opera (#2865)
32                                 if ( s.username ) {
33                                         xhr.open(s.type, s.url, s.async, s.username, s.password);
34                                 } else {
35                                         xhr.open(s.type, s.url, s.async);
36                                 }
37
38                                 // Requested-With header
39                                 // Not set for crossDomain requests with no content
40                                 // (see why at http://trac.dojotoolkit.org/ticket/9486)
41                                 // Won't change header if already provided in beforeSend
42                                 if ( ! ( s.crossDomain && ! s.hasContent ) && ! headers["x-requested-with"] ) {
43                                         headers["x-requested-with"] = "XMLHttpRequest";
44                                 }
45
46                                 // Need an extra try/catch for cross domain requests in Firefox 3
47                                 try {
48
49                                         jQuery.each(headers, function(key,value) {
50                                                 xhr.setRequestHeader(key,value);
51                                         });
52
53                                 } catch(_) {}
54
55                                 // Do send the request
56                                 try {
57                                         xhr.send( ( s.hasContent && s.data ) || null );
58                                 } catch(e) {
59                                         // Store back in pool
60                                         xhrPool.push( xhr );
61                                         complete(0, "error", "" + e);
62                                         return;
63                                 }
64
65                                 // Listener
66                                 callback = function ( abortStatusText ) {
67
68                                         // Was never called and is aborted or complete
69                                         if ( callback && ( abortStatusText || xhr.readyState === 4 ) ) {
70
71                                                 // Do not listen anymore
72                                                 // and Store back in pool
73                                                 if (handle) {
74                                                         xhr.onreadystatechange = jQuery.noop;
75                                                         delete xhrs[ handle ];
76                                                         handle = undefined;
77                                                         xhrPool.push( xhr );
78                                                 }
79
80                                                 callback = 0;
81
82                                                 // Get info
83                                                 var status, statusText, response, responseHeaders;
84
85                                                 if ( abortStatusText ) {
86
87                                                         if ( xhr.readyState !== 4 ) {
88                                                                 xhr.abort();
89                                                         }
90
91                                                         // Stop here if unloadAbort
92                                                         if ( abortStatusText === xhrUnloadAbortMarker ) {
93                                                                 return;
94                                                         }
95
96                                                         status = 0;
97                                                         statusText = abortStatusText;
98
99                                                 } else {
100
101                                                         status = xhr.status;
102
103                                                         try { // Firefox throws an exception when accessing statusText for faulty cross-domain requests
104
105                                                                 statusText = xhr.statusText;
106
107                                                         } catch( e ) {
108
109                                                                 statusText = ""; // We normalize with Webkit giving an empty statusText
110
111                                                         }
112
113                                                         responseHeaders = xhr.getAllResponseHeaders();
114
115                                                         // Filter status for non standard behaviours
116                                                         // (so many they seem to be the actual "standard")
117                                                         status =
118                                                                 // Opera returns 0 when it should be 304
119                                                                 // Webkit returns 0 for failing cross-domain no matter the real status
120                                                                 status === 0 ?
121                                                                         (
122                                                                                 ! s.crossDomain || statusText ? // Webkit, Firefox: filter out faulty cross-domain requests
123                                                                                 (
124                                                                                         responseHeaders ? // Opera: filter out real aborts #6060
125                                                                                         304
126                                                                                         :
127                                                                                         0
128                                                                                 )
129                                                                                 :
130                                                                                 302 // We assume 302 but could be anything cross-domain related
131                                                                         )
132                                                                         :
133                                                                         (
134                                                                                 status == 1223 ?        // IE sometimes returns 1223 when it should be 204 (see #1450)
135                                                                                         204
136                                                                                         :
137                                                                                         status
138                                                                         );
139
140                                                         // Guess response if needed & update datatype accordingly
141                                                         if ( status >= 200 && status < 300 ) {
142                                                                 response =
143                                                                         determineDataType(
144                                                                                 s,
145                                                                                 xhr.getResponseHeader("content-type"),
146                                                                                 xhr.responseText,
147                                                                                 xhr.responseXML );
148                                                         }
149                                                 }
150
151                                                 // Call complete
152                                                 complete(status,statusText,response,responseHeaders);
153                                         }
154                                 };
155
156                                 // if we're in sync mode
157                                 // or it's in cache and has been retrieved directly (IE6 & IE7)
158                                 // we need to manually fire the callback
159                                 if ( ! s.async || xhr.readyState === 4 ) {
160
161                                         callback();
162
163                                 } else {
164
165                                         // Listener is externalized to handle abort on unload
166                                         handle = xhrPollingId++;
167                                         xhrs[ handle ] = xhr;
168                                         xhr.onreadystatechange = function() {
169                                                 callback();
170                                         };
171                                 }
172                         },
173
174                         abort: function(statusText) {
175                                 if ( callback ) {
176                                         callback(statusText);
177                                 }
178                         }
179                 };
180         }
181 });
182
183 // #5280: we need to abort on unload or IE will keep connections alive
184 jQuery(window).bind( "unload" , function() {
185
186         // Abort all pending requests
187         jQuery.each(xhrs, function(_, xhr) {
188                 if ( xhr.onreadystatechange ) {
189                         xhr.onreadystatechange( xhrUnloadAbortMarker );
190                 }
191         });
192
193         // Resest polling structure to be safe
194         xhrs = {};
195
196 });
197
198 })( jQuery );