From 9558c8e2e91c956710aa54be1960ce4699fced20 Mon Sep 17 00:00:00 2001 From: Bryce Gibson Date: Fri, 10 Feb 2017 18:13:07 +1100 Subject: [PATCH] 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. --- request/request.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/request/request.js b/request/request.js index b153b0e9..c05ec0a3 100644 --- a/request/request.js +++ b/request/request.js @@ -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) {