5 ], function( jQuery, support ) {
9 jQuery.ajaxSettings.xhr = function() {
11 return new window.XMLHttpRequest();
15 var xhrSuccessStatus = {
17 // File protocol always yields status code 0, assume 200
20 // Support: IE <=9 only
21 // #1450: sometimes IE returns 1223 when it should be 204
24 xhrSupported = jQuery.ajaxSettings.xhr();
26 support.cors = !!xhrSupported && ( "withCredentials" in xhrSupported );
27 support.ajax = xhrSupported = !!xhrSupported;
29 jQuery.ajaxTransport( function( options ) {
30 var callback, errorCallback;
32 // Cross domain only allowed if supported through XMLHttpRequest
33 if ( support.cors || xhrSupported && !options.crossDomain ) {
35 send: function( headers, complete ) {
47 // Apply custom fields if provided
48 if ( options.xhrFields ) {
49 for ( i in options.xhrFields ) {
50 xhr[ i ] = options.xhrFields[ i ];
54 // Override mime type if needed
55 if ( options.mimeType && xhr.overrideMimeType ) {
56 xhr.overrideMimeType( options.mimeType );
59 // X-Requested-With header
60 // For cross-domain requests, seeing as conditions for a preflight are
61 // akin to a jigsaw puzzle, we simply never set it to be sure.
62 // (it can always be set on a per-request basis or even using ajaxSetup)
63 // For same-domain requests, won't change header if already provided.
64 if ( !options.crossDomain && !headers[ "X-Requested-With" ] ) {
65 headers[ "X-Requested-With" ] = "XMLHttpRequest";
69 for ( i in headers ) {
70 xhr.setRequestHeader( i, headers[ i ] );
74 callback = function( type ) {
77 callback = errorCallback = xhr.onload =
78 xhr.onerror = xhr.onabort = xhr.onreadystatechange = null;
80 if ( type === "abort" ) {
82 } else if ( type === "error" ) {
84 // Support: IE <=9 only
85 // On a manual native abort, IE9 throws
86 // errors on any property access that is not readyState
87 if ( typeof xhr.status !== "number" ) {
88 complete( 0, "error" );
92 // File: protocol always yields status 0; see #8605, #14207
99 xhrSuccessStatus[ xhr.status ] || xhr.status,
102 // Support: IE <=9 only
103 // IE9 has no XHR2 but throws on binary (trac-11426)
104 // For XHR2 non-text, let the caller handle it (gh-2498)
105 ( xhr.responseType || "text" ) !== "text" ||
106 typeof xhr.responseText !== "string" ?
107 { binary: xhr.response } :
108 { text: xhr.responseText },
109 xhr.getAllResponseHeaders()
117 xhr.onload = callback();
118 errorCallback = xhr.onerror = callback( "error" );
120 // Support: IE 9 only
121 // Use onreadystatechange to replace onabort
122 // to handle uncaught aborts
123 if ( xhr.onabort !== undefined ) {
124 xhr.onabort = errorCallback;
126 xhr.onreadystatechange = function() {
128 // Check readyState before timeout as it changes
129 if ( xhr.readyState === 4 ) {
131 // Allow onerror to be called first,
132 // but that will not handle a native abort
133 // Also, save errorCallback to a variable
134 // as xhr.onerror cannot be accessed
135 window.setTimeout( function() {
144 // Create the abort callback
145 callback = callback( "abort" );
149 // Do send the request (this may raise an exception)
150 xhr.send( options.hasContent && options.data || null );
153 // #14683: Only rethrow if this hasn't been notified as an error yet