Use xhr.onreadystatechange instead of xhr.onload (#1838)

This commit is contained in:
David Fowler 2018-04-03 15:53:16 -07:00 committed by GitHub
parent 2a71d18a6a
commit 5e38303377
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 8 deletions

View File

@ -84,16 +84,23 @@ export class DefaultHttpClient extends HttpClient {
xhr.timeout = request.timeout; xhr.timeout = request.timeout;
} }
xhr.onload = () => { xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
if (request.abortSignal) { if (request.abortSignal) {
request.abortSignal.onabort = null; request.abortSignal.onabort = null;
} }
if (xhr.status >= 200 && xhr.status < 300) { // Some browsers report xhr.status == 0 when the
// response has been cut off or there's been a TCP FIN.
// Treat it like a 200 with no response.
if (xhr.status === 0) {
resolve(new HttpResponse(200, null, null));
} else if (xhr.status >= 200 && xhr.status < 300) {
resolve(new HttpResponse(xhr.status, xhr.statusText, xhr.response || xhr.responseText)); resolve(new HttpResponse(xhr.status, xhr.statusText, xhr.response || xhr.responseText));
} else { } else {
reject(new HttpError(xhr.statusText, xhr.status)); reject(new HttpError(xhr.statusText, xhr.status));
} }
}
}; };
xhr.onerror = () => { xhr.onerror = () => {