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.ontimeout =
79 xhr.onreadystatechange = null;
81 if ( type === "abort" ) {
83 } else if ( type === "error" ) {
85 // Support: IE <=9 only
86 // On a manual native abort, IE9 throws
87 // errors on any property access that is not readyState
88 if ( typeof xhr.status !== "number" ) {
89 complete( 0, "error" );
93 // File: protocol always yields status 0; see #8605, #14207
100 xhrSuccessStatus[ xhr.status ] || xhr.status,
103 // Support: IE <=9 only
104 // IE9 has no XHR2 but throws on binary (trac-11426)
105 // For XHR2 non-text, let the caller handle it (gh-2498)
106 ( xhr.responseType || "text" ) !== "text" ||
107 typeof xhr.responseText !== "string" ?
108 { binary: xhr.response } :
109 { text: xhr.responseText },
110 xhr.getAllResponseHeaders()
118 xhr.onload = callback();
119 errorCallback = xhr.onerror = xhr.ontimeout = callback( "error" );
121 // Support: IE 9 only
122 // Use onreadystatechange to replace onabort
123 // to handle uncaught aborts
124 if ( xhr.onabort !== undefined ) {
125 xhr.onabort = errorCallback;
127 xhr.onreadystatechange = function() {
129 // Check readyState before timeout as it changes
130 if ( xhr.readyState === 4 ) {
132 // Allow onerror to be called first,
133 // but that will not handle a native abort
134 // Also, save errorCallback to a variable
135 // as xhr.onerror cannot be accessed
136 window.setTimeout( function() {
145 // Create the abort callback
146 callback = callback( "abort" );
150 // Do send the request (this may raise an exception)
151 xhr.send( options.hasContent && options.data || null );
154 // #14683: Only rethrow if this hasn't been notified as an error yet