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

Commit 4f57236

Browse files
committed
fix($httpBackend): Ignore multiple calls to onreadystatechange with readyState=4
On mobile webkit `onreadystatechange` might by called multiple times with `readyState===4` caused by xhrs that are resolved while the app is in the background. Fixes #5426.
1 parent 50bf029 commit 4f57236

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/ng/httpBackend.js

+5
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument)
7070
// always async
7171
xhr.onreadystatechange = function() {
7272
if (xhr.readyState == 4) {
73+
// onreadystatechange might by called multiple times
74+
// with readyState === 4 on mobile webkit caused by
75+
// xhrs that are resolved while the app is in the background (see #5426).
76+
xhr.onreadystatechange = undefined;
77+
7378
var responseHeaders = null,
7479
response = null;
7580

test/ng/httpBackendSpec.js

+12
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,18 @@ describe('$httpBackend', function() {
9090
expect(callback).toHaveBeenCalledOnce();
9191
});
9292

93+
// onreadystatechange might by called multiple times
94+
// with readyState === 4 on mobile webkit caused by
95+
// xhrs that are resolved while the app is in the background (see #5426).
96+
it('should remove onreadystatechange when it is called with readyState=4 to ignore multiple calls', function() {
97+
$backend('GET', 'URL', null, callback);
98+
xhr = MockXhr.$$lastInstance;
99+
100+
xhr.status = 200;
101+
xhr.readyState = 4;
102+
xhr.onreadystatechange();
103+
expect(xhr.onreadystatechange).toBeUndefined();
104+
});
93105

94106
it('should set only the requested headers', function() {
95107
$backend('POST', 'URL', null, noop, {'X-header1': 'value1', 'X-header2': 'value2'});

0 commit comments

Comments
 (0)