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

Commit 3d38fff

Browse files
committed
fix($httpBackend): don't delete xhr.onreadystatechange otherwise Safari :-O
1 parent bc492c0 commit 3d38fff

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

src/ng/httpBackend.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc
7171
// response is in the cache. the promise api will ensure that to the app code the api is
7272
// always async
7373
xhr.onreadystatechange = function() {
74-
if (xhr.readyState == 4) {
75-
// onreadystatechange might by called multiple times
76-
// with readyState === 4 on mobile webkit caused by
77-
// xhrs that are resolved while the app is in the background (see #5426).
78-
//
79-
// we must delete the property instead of setting it to undefined/null to make IE8 happy.
80-
delete xhr.onreadystatechange;
81-
74+
// onreadystatechange might by called multiple times with readyState === 4 on mobile webkit caused by
75+
// xhrs that are resolved while the app is in the background (see #5426).
76+
// since calling completeRequest sets the `xhr` variable to null, we just check if it's not null before
77+
// continuing
78+
//
79+
// we can't set xhr.onreadystatechange to undefined or delete it because that breaks IE8 (method=PATCH) and
80+
// Safari respectively.
81+
if (xhr && xhr.readyState == 4) {
8282
var responseHeaders = null,
8383
response = null;
8484

test/ng/httpBackendSpec.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,16 @@ describe('$httpBackend', function() {
9393
// onreadystatechange might by called multiple times
9494
// with readyState === 4 on mobile webkit caused by
9595
// 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() {
96+
it('should not process onreadystatechange callback with readyState == 4 more than once', function() {
9797
$backend('GET', 'URL', null, callback);
9898
xhr = MockXhr.$$lastInstance;
9999

100100
xhr.status = 200;
101101
xhr.readyState = 4;
102102
xhr.onreadystatechange();
103-
expect(xhr.onreadystatechange).toBeUndefined();
103+
xhr.onreadystatechange();
104+
105+
expect(callback).toHaveBeenCalledOnce();
104106
});
105107

106108
it('should set only the requested headers', function() {

0 commit comments

Comments
 (0)