Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Error thrown in xhr.onreadystatechange when javascript execution is suspended/resumed on iOS #6582

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/ng/httpBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc
// we can't set xhr.onreadystatechange to undefined or delete it because that breaks IE8 (method=PATCH) and
// Safari respectively.
if (xhr && xhr.readyState == 4) {
// onreadystatechange might by called multiple times
// with readyState === 4 on mobile webkit caused by
// xhrs that are resolved while the app is in the background (see #5426).
xhr.onreadystatechange = function(){};

var responseHeaders = null,
response = null;

Expand Down
18 changes: 18 additions & 0 deletions test/ng/httpBackendSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,24 @@ describe('$httpBackend', function() {
expect(callback).toHaveBeenCalledOnce();
});

// onreadystatechange might by called multiple times
// with readyState === 4 on mobile webkit caused by
// xhrs that are resolved while the app is in the background (see #5426).
it('should remove onreadystatechange when it is called with readyState=4 to ignore multiple calls', function() {
$backend('GET', 'URL', null, callback);
xhr = MockXhr.$$lastInstance;

xhr.status = 200;
xhr.readyState = 4;

var orig = xhr.onreadystatechange

xhr.onreadystatechange();
expect(xhr.onreadystatechange).toNotBe(orig);

xhr.onreadystatechange(); // empty function
});

// onreadystatechange might by called multiple times
// with readyState === 4 on mobile webkit caused by
// xhrs that are resolved while the app is in the background (see #5426).
Expand Down