Blame view
app/bower_components/jquery/src/ajax/xhr.js
4.2 KB
87c93a029
|
1 |
define( [ |
f986e111b
|
2 3 4 5 |
"../core", "../var/support", "../ajax" ], function( jQuery, support ) { |
87c93a029
|
6 |
"use strict"; |
f986e111b
|
7 |
|
87c93a029
|
8 9 10 11 12 |
jQuery.ajaxSettings.xhr = function() { try { return new window.XMLHttpRequest(); } catch ( e ) {} }; |
f986e111b
|
13 |
|
87c93a029
|
14 |
var xhrSuccessStatus = { |
f986e111b
|
15 |
|
87c93a029
|
16 17 |
// File protocol always yields status code 0, assume 200 0: 200, |
f986e111b
|
18 |
|
87c93a029
|
19 20 21 22 23 |
// Support: IE <=9 only // #1450: sometimes IE returns 1223 when it should be 204 1223: 204 }, xhrSupported = jQuery.ajaxSettings.xhr(); |
f986e111b
|
24 |
|
87c93a029
|
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
support.cors = !!xhrSupported && ( "withCredentials" in xhrSupported ); support.ajax = xhrSupported = !!xhrSupported; jQuery.ajaxTransport( function( options ) { var callback, errorCallback; // Cross domain only allowed if supported through XMLHttpRequest if ( support.cors || xhrSupported && !options.crossDomain ) { return { send: function( headers, complete ) { var i, xhr = options.xhr(); xhr.open( options.type, options.url, options.async, options.username, options.password ); // Apply custom fields if provided if ( options.xhrFields ) { for ( i in options.xhrFields ) { xhr[ i ] = options.xhrFields[ i ]; |
f986e111b
|
50 |
} |
87c93a029
|
51 |
} |
f986e111b
|
52 |
|
87c93a029
|
53 54 55 56 |
// Override mime type if needed if ( options.mimeType && xhr.overrideMimeType ) { xhr.overrideMimeType( options.mimeType ); } |
f986e111b
|
57 |
|
87c93a029
|
58 59 60 61 62 63 64 65 |
// X-Requested-With header // For cross-domain requests, seeing as conditions for a preflight are // akin to a jigsaw puzzle, we simply never set it to be sure. // (it can always be set on a per-request basis or even using ajaxSetup) // For same-domain requests, won't change header if already provided. if ( !options.crossDomain && !headers[ "X-Requested-With" ] ) { headers[ "X-Requested-With" ] = "XMLHttpRequest"; } |
f986e111b
|
66 |
|
87c93a029
|
67 68 69 70 |
// Set headers for ( i in headers ) { xhr.setRequestHeader( i, headers[ i ] ); } |
f986e111b
|
71 |
|
87c93a029
|
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
// Callback callback = function( type ) { return function() { if ( callback ) { callback = errorCallback = xhr.onload = xhr.onerror = xhr.onabort = xhr.onreadystatechange = null; if ( type === "abort" ) { xhr.abort(); } else if ( type === "error" ) { // Support: IE <=9 only // On a manual native abort, IE9 throws // errors on any property access that is not readyState if ( typeof xhr.status !== "number" ) { complete( 0, "error" ); } else { complete( // File: protocol always yields status 0; see #8605, #14207 xhr.status, xhr.statusText ); |
f986e111b
|
95 96 |
} } else { |
87c93a029
|
97 98 99 100 101 102 103 104 105 106 107 108 109 |
complete( xhrSuccessStatus[ xhr.status ] || xhr.status, xhr.statusText, // Support: IE <=9 only // IE9 has no XHR2 but throws on binary (trac-11426) // For XHR2 non-text, let the caller handle it (gh-2498) ( xhr.responseType || "text" ) !== "text" || typeof xhr.responseText !== "string" ? { binary: xhr.response } : { text: xhr.responseText }, xhr.getAllResponseHeaders() ); |
f986e111b
|
110 111 |
} } |
87c93a029
|
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
}; }; // Listen to events xhr.onload = callback(); errorCallback = xhr.onerror = callback( "error" ); // Support: IE 9 only // Use onreadystatechange to replace onabort // to handle uncaught aborts if ( xhr.onabort !== undefined ) { xhr.onabort = errorCallback; } else { xhr.onreadystatechange = function() { // Check readyState before timeout as it changes if ( xhr.readyState === 4 ) { // Allow onerror to be called first, // but that will not handle a native abort // Also, save errorCallback to a variable // as xhr.onerror cannot be accessed window.setTimeout( function() { if ( callback ) { errorCallback(); } } ); |
f986e111b
|
139 140 |
} }; |
87c93a029
|
141 |
} |
f986e111b
|
142 |
|
87c93a029
|
143 144 145 146 |
// Create the abort callback callback = callback( "abort" ); try { |
f986e111b
|
147 |
|
87c93a029
|
148 149 150 151 152 |
// Do send the request (this may raise an exception) xhr.send( options.hasContent && options.data || null ); } catch ( e ) { // #14683: Only rethrow if this hasn't been notified as an error yet |
f986e111b
|
153 |
if ( callback ) { |
87c93a029
|
154 |
throw e; |
f986e111b
|
155 156 |
} } |
87c93a029
|
157 |
}, |
f986e111b
|
158 |
|
87c93a029
|
159 160 161 162 163 164 165 166 |
abort: function() { if ( callback ) { callback(); } } }; } } ); |
f986e111b
|
167 |
|
87c93a029
|
168 |
} ); |