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:
parent
a85f595dcd
commit
9558c8e2e9
1 changed files with 14 additions and 4 deletions
|
|
@ -53,7 +53,16 @@ module.exports = function($window, Promise) {
|
||||||
if (useBody) args.data = args.serialize(args.data)
|
if (useBody) args.data = args.serialize(args.data)
|
||||||
else args.url = assemble(args.url, 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)
|
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) {
|
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
|
if (typeof args.config === "function") xhr = args.config(xhr, args) || xhr
|
||||||
|
|
||||||
xhr.onreadystatechange = function() {
|
xhr.onreadystatechange = function() {
|
||||||
// Don't throw errors on xhr.abort(). XMLHttpRequests ends up in a state of
|
// Don't throw errors on xhr.abort().
|
||||||
// xhr.status == 0 and xhr.readyState == 4 if aborted after open, but before completion.
|
if(aborted) return
|
||||||
if (xhr.status && xhr.readyState === 4) {
|
|
||||||
|
if (xhr.readyState === 4) {
|
||||||
try {
|
try {
|
||||||
var response = (args.extract !== extract) ? args.extract(xhr, args) : args.deserialize(args.extract(xhr, args))
|
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) {
|
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status === 304) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue