Avoid inaccurately inferring xhr abort.

xhr.status can equal zero in non-abort scenarios, eg timeout or CORS
failure.

Instead use a variable to track aborts.
This commit is contained in:
Bryce Gibson 2017-02-10 18:13:07 +11:00
parent a85f595dcd
commit 9558c8e2e9

View file

@ -53,7 +53,16 @@ module.exports = function($window, Promise) {
if (useBody) args.data = args.serialize(args.data)
else args.url = assemble(args.url, args.data)
var xhr = new $window.XMLHttpRequest()
var xhr = new $window.XMLHttpRequest(),
aborted = false,
_abort = xhr.abort
xhr.abort = function abort() {
aborted = true
_abort.call(xhr)
}
xhr.open(args.method, args.url, typeof args.async === "boolean" ? args.async : true, typeof args.user === "string" ? args.user : undefined, typeof args.password === "string" ? args.password : undefined)
if (args.serialize === JSON.stringify && useBody) {
@ -71,9 +80,10 @@ module.exports = function($window, Promise) {
if (typeof args.config === "function") xhr = args.config(xhr, args) || xhr
xhr.onreadystatechange = function() {
// Don't throw errors on xhr.abort(). XMLHttpRequests ends up in a state of
// xhr.status == 0 and xhr.readyState == 4 if aborted after open, but before completion.
if (xhr.status && xhr.readyState === 4) {
// Don't throw errors on xhr.abort().
if(aborted) return
if (xhr.readyState === 4) {
try {
var response = (args.extract !== extract) ? args.extract(xhr, args) : args.deserialize(args.extract(xhr, args))
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status === 304) {