| 1 |
(function( jQuery ) { |
| 2 |
|
| 3 |
var // #5280: Internet Explorer will keep connections alive if we don't abort on unload |
| 4 |
xhrOnUnloadAbort = window.ActiveXObject ? function() { |
| 5 |
// Abort all pending requests |
| 6 |
for ( var key in xhrCallbacks ) { |
| 7 |
xhrCallbacks[ key ]( 0, 1 ); |
| 8 |
} |
| 9 |
} : false, |
| 10 |
xhrId = 0, |
| 11 |
xhrCallbacks; |
| 12 |
|
| 13 |
// Functions to create xhrs |
| 14 |
function createStandardXHR() { |
| 15 |
try { |
| 16 |
return new window.XMLHttpRequest(); |
| 17 |
} catch( e ) {} |
| 18 |
} |
| 19 |
|
| 20 |
function createActiveXHR() { |
| 21 |
try { |
| 22 |
return new window.ActiveXObject( "Microsoft.XMLHTTP" ); |
| 23 |
} catch( e ) {} |
| 24 |
} |
| 25 |
|
| 26 |
// Create the request object |
| 27 |
// (This is still attached to ajaxSettings for backward compatibility) |
| 28 |
jQuery.ajaxSettings.xhr = window.ActiveXObject ? |
| 29 |
/* Microsoft failed to properly |
| 30 |
* implement the XMLHttpRequest in IE7 (can't request local files), |
| 31 |
* so we use the ActiveXObject when it is available |
| 32 |
* Additionally XMLHttpRequest can be disabled in IE7/IE8 so |
| 33 |
* we need a fallback. |
| 34 |
*/ |
| 35 |
function() { |
| 36 |
return !this.isLocal && createStandardXHR() || createActiveXHR(); |
| 37 |
} : |
| 38 |
// For all other browsers, use the standard XMLHttpRequest object |
| 39 |
createStandardXHR; |
| 40 |
|
| 41 |
// Determine support properties |
| 42 |
(function( xhr ) { |
| 43 |
jQuery.extend( jQuery.support, { |
| 44 |
ajax: !!xhr, |
| 45 |
cors: !!xhr && ( "withCredentials" in xhr ) |
| 46 |
}); |
| 47 |
})( jQuery.ajaxSettings.xhr() ); |
| 48 |
|
| 49 |
// Create transport if the browser can provide an xhr |
| 50 |
if ( jQuery.support.ajax ) { |
| 51 |
|
| 52 |
jQuery.ajaxTransport(function( s ) { |
| 53 |
// Cross domain only allowed if supported through XMLHttpRequest |
| 54 |
if ( !s.crossDomain || jQuery.support.cors ) { |
| 55 |
|
| 56 |
var callback; |
| 57 |
|
| 58 |
return { |
| 59 |
send: function( headers, complete ) { |
| 60 |
|
| 61 |
// Get a new xhr |
| 62 |
var xhr = s.xhr(), |
| 63 |
handle, |
| 64 |
i; |
| 65 |
|
| 66 |
// Open the socket |
| 67 |
// Passing null username, generates a login popup on Opera (#2865) |
| 68 |
if ( s.username ) { |
| 69 |
xhr.open( s.type, s.url, s.async, s.username, s.password ); |
| 70 |
} else { |
| 71 |
xhr.open( s.type, s.url, s.async ); |
| 72 |
} |
| 73 |
|
| 74 |
// Apply custom fields if provided |
| 75 |
if ( s.xhrFields ) { |
| 76 |
for ( i in s.xhrFields ) { |
| 77 |
xhr[ i ] = s.xhrFields[ i ]; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
// Override mime type if needed |
| 82 |
if ( s.mimeType && xhr.overrideMimeType ) { |
| 83 |
xhr.overrideMimeType( s.mimeType ); |
| 84 |
} |
| 85 |
|
| 86 |
// X-Requested-With header |
| 87 |
// For cross-domain requests, seeing as conditions for a preflight are |
| 88 |
// akin to a jigsaw puzzle, we simply never set it to be sure. |
| 89 |
// (it can always be set on a per-request basis or even using ajaxSetup) |
| 90 |
// For same-domain requests, won't change header if already provided. |
| 91 |
if ( !s.crossDomain && !headers["X-Requested-With"] ) { |
| 92 |
headers[ "X-Requested-With" ] = "XMLHttpRequest"; |
| 93 |
} |
| 94 |
|
| 95 |
// Need an extra try/catch for cross domain requests in Firefox 3 |
| 96 |
try { |
| 97 |
for ( i in headers ) { |
| 98 |
xhr.setRequestHeader( i, headers[ i ] ); |
| 99 |
} |
| 100 |
} catch( _ ) {} |
| 101 |
|
| 102 |
// Do send the request |
| 103 |
// This may raise an exception which is actually |
| 104 |
// handled in jQuery.ajax (so no try/catch here) |
| 105 |
xhr.send( ( s.hasContent && s.data ) || null ); |
| 106 |
|
| 107 |
// Listener |
| 108 |
callback = function( _, isAbort ) { |
| 109 |
|
| 110 |
var status, |
| 111 |
statusText, |
| 112 |
responseHeaders, |
| 113 |
responses, |
| 114 |
xml; |
| 115 |
|
| 116 |
// Firefox throws exceptions when accessing properties |
| 117 |
// of an xhr when a network error occured |
| 118 |
// http://helpful.knobs-dials.com/index.php/Component_returned_failure_code:_0x80040111_(NS_ERROR_NOT_AVAILABLE) |
| 119 |
try { |
| 120 |
|
| 121 |
// Was never called and is aborted or complete |
| 122 |
if ( callback && ( isAbort || xhr.readyState === 4 ) ) { |
| 123 |
|
| 124 |
// Only called once |
| 125 |
callback = undefined; |
| 126 |
|
| 127 |
// Do not keep as active anymore |
| 128 |
if ( handle ) { |
| 129 |
xhr.onreadystatechange = jQuery.noop; |
| 130 |
if ( xhrOnUnloadAbort ) { |
| 131 |
delete xhrCallbacks[ handle ]; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
// If it's an abort |
| 136 |
if ( isAbort ) { |
| 137 |
// Abort it manually if needed |
| 138 |
if ( xhr.readyState !== 4 ) { |
| 139 |
xhr.abort(); |
| 140 |
} |
| 141 |
} else { |
| 142 |
status = xhr.status; |
| 143 |
responseHeaders = xhr.getAllResponseHeaders(); |
| 144 |
responses = {}; |
| 145 |
xml = xhr.responseXML; |
| 146 |
|
| 147 |
// Construct response list |
| 148 |
if ( xml && xml.documentElement /* #4958 */ ) { |
| 149 |
responses.xml = xml; |
| 150 |
} |
| 151 |
responses.text = xhr.responseText; |
| 152 |
|
| 153 |
// Firefox throws an exception when accessing |
| 154 |
// statusText for faulty cross-domain requests |
| 155 |
try { |
| 156 |
statusText = xhr.statusText; |
| 157 |
} catch( e ) { |
| 158 |
// We normalize with Webkit giving an empty statusText |
| 159 |
statusText = ""; |
| 160 |
} |
| 161 |
|
| 162 |
// Filter status for non standard behaviors |
| 163 |
|
| 164 |
// If the request is local and we have data: assume a success |
| 165 |
// (success with no data won't get notified, that's the best we |
| 166 |
// can do given current implementations) |
| 167 |
if ( !status && s.isLocal && !s.crossDomain ) { |
| 168 |
status = responses.text ? 200 : 404; |
| 169 |
// IE - #1450: sometimes returns 1223 when it should be 204 |
| 170 |
} else if ( status === 1223 ) { |
| 171 |
status = 204; |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
} catch( firefoxAccessException ) { |
| 176 |
if ( !isAbort ) { |
| 177 |
complete( -1, firefoxAccessException ); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
// Call complete if needed |
| 182 |
if ( responses ) { |
| 183 |
complete( status, statusText, responses, responseHeaders ); |
| 184 |
} |
| 185 |
}; |
| 186 |
|
| 187 |
// if we're in sync mode or it's in cache |
| 188 |
// and has been retrieved directly (IE6 & IE7) |
| 189 |
// we need to manually fire the callback |
| 190 |
if ( !s.async || xhr.readyState === 4 ) { |
| 191 |
callback(); |
| 192 |
} else { |
| 193 |
handle = ++xhrId; |
| 194 |
if ( xhrOnUnloadAbort ) { |
| 195 |
// Create the active xhrs callbacks list if needed |
| 196 |
// and attach the unload handler |
| 197 |
if ( !xhrCallbacks ) { |
| 198 |
xhrCallbacks = {}; |
| 199 |
jQuery( window ).unload( xhrOnUnloadAbort ); |
| 200 |
} |
| 201 |
// Add to list of active xhrs callbacks |
| 202 |
xhrCallbacks[ handle ] = callback; |
| 203 |
} |
| 204 |
xhr.onreadystatechange = callback; |
| 205 |
} |
| 206 |
}, |
| 207 |
|
| 208 |
abort: function() { |
| 209 |
if ( callback ) { |
| 210 |
callback(0,1); |
| 211 |
} |
| 212 |
} |
| 213 |
}; |
| 214 |
} |
| 215 |
}); |
| 216 |
} |
| 217 |
|
| 218 |
})( jQuery ); |