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

Commit 3ae3ccf

Browse files
vojtajinaIgorMinar
authored andcommitted
fix($browser.xhr): fix IE6, IE7 bug - sync xhr when serving from cache
IE6, IE7 is sync when serving content from cache. We want consistent api, so we have to use setTimeout to make it async.
1 parent e9b57f9 commit 3ae3ccf

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

src/service/browser.js

+20-7
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ function Browser(window, document, body, XHR, $log, $sniffer) {
7373
}
7474
}
7575

76+
// normalize IE bug (http://bugs.jquery.com/ticket/1450)
77+
function fixStatus(status) {
78+
return status == 1223 ? 204 : status;
79+
}
80+
7681
/**
7782
* @ngdoc method
7883
* @name angular.module.ng.$browser#xhr
@@ -120,14 +125,22 @@ function Browser(window, document, body, XHR, $log, $sniffer) {
120125
forEach(headers, function(value, key) {
121126
if (value) xhr.setRequestHeader(key, value);
122127
});
123-
xhr.onreadystatechange = function() {
124-
if (xhr.readyState == 4) {
125-
// normalize IE bug (http://bugs.jquery.com/ticket/1450)
126-
var status = xhr.status == 1223 ? 204 : xhr.status;
127-
completeOutstandingRequest(callback, status, xhr.responseText);
128-
}
129-
};
128+
130129
xhr.send(post || '');
130+
131+
// IE6, IE7 bug - does sync when serving from cache
132+
if (xhr.readyState == 4) {
133+
setTimeout(function() {
134+
completeOutstandingRequest(callback, fixStatus(xhr.status), xhr.responseText);
135+
}, 0);
136+
} else {
137+
xhr.onreadystatechange = function() {
138+
if (xhr.readyState == 4) {
139+
completeOutstandingRequest(callback, fixStatus(xhr.status), xhr.responseText);
140+
}
141+
};
142+
}
143+
131144
return xhr;
132145
}
133146
};

test/service/browserSpecs.js

+29
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,35 @@ describe('browser', function() {
227227
it('should return raw xhr object', function() {
228228
expect(browser.xhr('GET', '/url', null, noop)).toBe(xhr);
229229
});
230+
231+
it('should be async even if xhr.send() is sync', function() {
232+
// IE6, IE7 is sync when serving from cache
233+
var xhr;
234+
function FakeXhr() {
235+
xhr = this;
236+
this.open = this.setRequestHeader = noop;
237+
this.send = function() {
238+
this.status = 200;
239+
this.responseText = 'response';
240+
this.readyState = 4;
241+
};
242+
}
243+
244+
var callback = jasmine.createSpy('done').andCallFake(function(status, response) {
245+
expect(status).toBe(200);
246+
expect(response).toBe('response');
247+
});
248+
249+
browser = new Browser(fakeWindow, jqLite(window.document), null, FakeXhr, null);
250+
browser.xhr('GET', '/url', null, callback);
251+
expect(callback).not.toHaveBeenCalled();
252+
253+
fakeWindow.setTimeout.flush();
254+
expect(callback).toHaveBeenCalledOnce();
255+
256+
(xhr.onreadystatechange || noop)();
257+
expect(callback).toHaveBeenCalledOnce();
258+
});
230259
});
231260

232261
describe('defer', function() {

0 commit comments

Comments
 (0)