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