| 1 |
import { jQuery } from "../core.js"; |
| 2 |
|
| 3 |
import "../ajax.js"; |
| 4 |
|
| 5 |
jQuery.ajaxSettings.xhr = function() { |
| 6 |
return new window.XMLHttpRequest(); |
| 7 |
}; |
| 8 |
|
| 9 |
var xhrSuccessStatus = { |
| 10 |
|
| 11 |
// File protocol always yields status code 0, assume 200 |
| 12 |
0: 200 |
| 13 |
}; |
| 14 |
|
| 15 |
jQuery.ajaxTransport( function( options ) { |
| 16 |
var callback; |
| 17 |
|
| 18 |
return { |
| 19 |
send: function( headers, complete ) { |
| 20 |
var i, |
| 21 |
xhr = options.xhr(); |
| 22 |
|
| 23 |
xhr.open( |
| 24 |
options.type, |
| 25 |
options.url, |
| 26 |
options.async, |
| 27 |
options.username, |
| 28 |
options.password |
| 29 |
); |
| 30 |
|
| 31 |
// Apply custom fields if provided |
| 32 |
if ( options.xhrFields ) { |
| 33 |
for ( i in options.xhrFields ) { |
| 34 |
xhr[ i ] = options.xhrFields[ i ]; |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
// Override mime type if needed |
| 39 |
if ( options.mimeType && xhr.overrideMimeType ) { |
| 40 |
xhr.overrideMimeType( options.mimeType ); |
| 41 |
} |
| 42 |
|
| 43 |
// X-Requested-With header |
| 44 |
// For cross-domain requests, seeing as conditions for a preflight are |
| 45 |
// akin to a jigsaw puzzle, we simply never set it to be sure. |
| 46 |
// (it can always be set on a per-request basis or even using ajaxSetup) |
| 47 |
// For same-domain requests, won't change header if already provided. |
| 48 |
if ( !options.crossDomain && !headers[ "X-Requested-With" ] ) { |
| 49 |
headers[ "X-Requested-With" ] = "XMLHttpRequest"; |
| 50 |
} |
| 51 |
|
| 52 |
// Set headers |
| 53 |
for ( i in headers ) { |
| 54 |
xhr.setRequestHeader( i, headers[ i ] ); |
| 55 |
} |
| 56 |
|
| 57 |
// Callback |
| 58 |
callback = function( type ) { |
| 59 |
return function() { |
| 60 |
if ( callback ) { |
| 61 |
callback = xhr.onload = xhr.onerror = xhr.onabort = xhr.ontimeout = null; |
| 62 |
|
| 63 |
if ( type === "abort" ) { |
| 64 |
xhr.abort(); |
| 65 |
} else if ( type === "error" ) { |
| 66 |
complete( |
| 67 |
|
| 68 |
// File: protocol always yields status 0; see trac-8605, trac-14207 |
| 69 |
xhr.status, |
| 70 |
xhr.statusText |
| 71 |
); |
| 72 |
} else { |
| 73 |
complete( |
| 74 |
xhrSuccessStatus[ xhr.status ] || xhr.status, |
| 75 |
xhr.statusText, |
| 76 |
|
| 77 |
// For XHR2 non-text, let the caller handle it (gh-2498) |
| 78 |
( xhr.responseType || "text" ) === "text" ? |
| 79 |
{ text: xhr.responseText } : |
| 80 |
{ binary: xhr.response }, |
| 81 |
xhr.getAllResponseHeaders() |
| 82 |
); |
| 83 |
} |
| 84 |
} |
| 85 |
}; |
| 86 |
}; |
| 87 |
|
| 88 |
// Listen to events |
| 89 |
xhr.onload = callback(); |
| 90 |
xhr.onabort = xhr.onerror = xhr.ontimeout = callback( "error" ); |
| 91 |
|
| 92 |
// Create the abort callback |
| 93 |
callback = callback( "abort" ); |
| 94 |
|
| 95 |
try { |
| 96 |
|
| 97 |
// Do send the request (this may raise an exception) |
| 98 |
xhr.send( options.hasContent && options.data || null ); |
| 99 |
} catch ( e ) { |
| 100 |
|
| 101 |
// trac-14683: Only rethrow if this hasn't been notified as an error yet |
| 102 |
if ( callback ) { |
| 103 |
throw e; |
| 104 |
} |
| 105 |
} |
| 106 |
}, |
| 107 |
|
| 108 |
abort: function() { |
| 109 |
if ( callback ) { |
| 110 |
callback(); |
| 111 |
} |
| 112 |
} |
| 113 |
}; |
| 114 |
} ); |