5 ], function( jQuery, support ) {
7 jQuery.ajaxSettings.xhr = function() {
9 return new XMLHttpRequest();
16 // 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();
25 // Open requests must be manually aborted on unload (#5280)
26 // See https://support.microsoft.com/kb/2856746 for more info
27 if ( window.attachEvent ) {
28 window.attachEvent( "onunload", function() {
29 for ( var key in xhrCallbacks ) {
30 xhrCallbacks[ key ]();
35 support.cors = !!xhrSupported && ( "withCredentials" in xhrSupported );
36 support.ajax = xhrSupported = !!xhrSupported;
38 jQuery.ajaxTransport(function( options ) {
41 // Cross domain only allowed if supported through XMLHttpRequest
42 if ( support.cors || xhrSupported && !options.crossDomain ) {
44 send: function( headers, complete ) {
49 xhr.open( options.type, options.url, options.async, options.username, options.password );
51 // Apply custom fields if provided
52 if ( options.xhrFields ) {
53 for ( i in options.xhrFields ) {
54 xhr[ i ] = options.xhrFields[ i ];
58 // Override mime type if needed
59 if ( options.mimeType && xhr.overrideMimeType ) {
60 xhr.overrideMimeType( options.mimeType );
63 // X-Requested-With header
64 // For cross-domain requests, seeing as conditions for a preflight are
65 // akin to a jigsaw puzzle, we simply never set it to be sure.
66 // (it can always be set on a per-request basis or even using ajaxSetup)
67 // For same-domain requests, won't change header if already provided.
68 if ( !options.crossDomain && !headers["X-Requested-With"] ) {
69 headers["X-Requested-With"] = "XMLHttpRequest";
73 for ( i in headers ) {
74 xhr.setRequestHeader( i, headers[ i ] );
78 callback = function( type ) {
81 delete xhrCallbacks[ id ];
82 callback = xhr.onload = xhr.onerror = null;
84 if ( type === "abort" ) {
86 } else if ( type === "error" ) {
88 // file: protocol always yields status 0; see #8605, #14207
94 xhrSuccessStatus[ xhr.status ] || xhr.status,
97 // Accessing binary-data responseText throws an exception
99 typeof xhr.responseText === "string" ? {
100 text: xhr.responseText
102 xhr.getAllResponseHeaders()
110 xhr.onload = callback();
111 xhr.onerror = callback("error");
113 // Create the abort callback
114 callback = xhrCallbacks[ id ] = callback("abort");
117 // Do send the request (this may raise an exception)
118 xhr.send( options.hasContent && options.data || null );
120 // #14683: Only rethrow if this hasn't been notified as an error yet