5 ], function( jQuery, support ) {
7 jQuery.ajaxSettings.xhr = function() {
9 return new window.XMLHttpRequest();
13 var xhrSuccessStatus = {
15 // File protocol always yields status code 0, assume 200
19 // #1450: sometimes IE returns 1223 when it should be 204
22 xhrSupported = jQuery.ajaxSettings.xhr();
24 support.cors = !!xhrSupported && ( "withCredentials" in xhrSupported );
25 support.ajax = xhrSupported = !!xhrSupported;
27 jQuery.ajaxTransport( function( options ) {
28 var callback, errorCallback;
30 // Cross domain only allowed if supported through XMLHttpRequest
31 if ( support.cors || xhrSupported && !options.crossDomain ) {
33 send: function( headers, complete ) {
45 // Apply custom fields if provided
46 if ( options.xhrFields ) {
47 for ( i in options.xhrFields ) {
48 xhr[ i ] = options.xhrFields[ i ];
52 // Override mime type if needed
53 if ( options.mimeType && xhr.overrideMimeType ) {
54 xhr.overrideMimeType( options.mimeType );
57 // X-Requested-With header
58 // For cross-domain requests, seeing as conditions for a preflight are
59 // akin to a jigsaw puzzle, we simply never set it to be sure.
60 // (it can always be set on a per-request basis or even using ajaxSetup)
61 // For same-domain requests, won't change header if already provided.
62 if ( !options.crossDomain && !headers[ "X-Requested-With" ] ) {
63 headers[ "X-Requested-With" ] = "XMLHttpRequest";
67 for ( i in headers ) {
68 xhr.setRequestHeader( i, headers[ i ] );
72 callback = function( type ) {
75 callback = errorCallback = xhr.onload =
76 xhr.onerror = xhr.onabort = xhr.onreadystatechange = null;
78 if ( type === "abort" ) {
80 } else if ( type === "error" ) {
83 // On a manual native abort, IE9 throws
84 // errors on any property access that is not readyState
85 if ( typeof xhr.status !== "number" ) {
86 complete( 0, "error" );
90 // File: protocol always yields status 0; see #8605, #14207
97 xhrSuccessStatus[ xhr.status ] || xhr.status,
101 // IE9 has no XHR2 but throws on binary (trac-11426)
102 // For XHR2 non-text, let the caller handle it (gh-2498)
103 ( xhr.responseType || "text" ) !== "text" ||
104 typeof xhr.responseText !== "string" ?
105 { binary: xhr.response } :
106 { text: xhr.responseText },
107 xhr.getAllResponseHeaders()
115 xhr.onload = callback();
116 errorCallback = xhr.onerror = callback( "error" );
119 // Use onreadystatechange to replace onabort
120 // to handle uncaught aborts
121 if ( xhr.onabort !== undefined ) {
122 xhr.onabort = errorCallback;
124 xhr.onreadystatechange = function() {
126 // Check readyState before timeout as it changes
127 if ( xhr.readyState === 4 ) {
129 // Allow onerror to be called first,
130 // but that will not handle a native abort
131 // Also, save errorCallback to a variable
132 // as xhr.onerror cannot be accessed
133 window.setTimeout( function() {
142 // Create the abort callback
143 callback = callback( "abort" );
147 // Do send the request (this may raise an exception)
148 xhr.send( options.hasContent && options.data || null );
151 // #14683: Only rethrow if this hasn't been notified as an error yet