Skip to content

Commit 6eb7ee5

Browse files
IgorMinarjamesdaily
authored andcommitted
fix($http): return responseText on IE8 for requests with responseType set
Closes angular#4464 Closes angular#4738 Closes angular#5636
1 parent 24d80a6 commit 6eb7ee5

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

Diff for: src/ng/httpBackend.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,12 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc
8484

8585
if(status !== ABORTED) {
8686
responseHeaders = xhr.getAllResponseHeaders();
87-
response = xhr.responseType ? xhr.response : xhr.responseText;
87+
88+
// responseText is the old-school way of retrieving response (supported by IE8 & 9)
89+
// response/responseType properties were introduced in XHR Level2 spec (supported by IE10)
90+
response = ('response' in xhr) ? xhr.response : xhr.responseText;
8891
}
8992

90-
// responseText is the old-school way of retrieving response (supported by IE8 & 9)
91-
// response/responseType properties were introduced in XHR Level2 spec (supported by IE10)
9293
completeRequest(callback,
9394
status || xhr.status,
9495
response,

Diff for: test/ng/httpBackendSpec.js

+34-10
Original file line numberDiff line numberDiff line change
@@ -264,21 +264,45 @@ describe('$httpBackend', function() {
264264
});
265265

266266

267-
it('should set responseType and return xhr.response', function() {
268-
$backend('GET', '/whatever', null, callback, {}, null, null, 'blob');
267+
describe('responseType', function() {
269268

270-
var xhrInstance = MockXhr.$$lastInstance;
271-
expect(xhrInstance.responseType).toBe('blob');
269+
it('should set responseType and return xhr.response', function() {
270+
$backend('GET', '/whatever', null, callback, {}, null, null, 'blob');
272271

273-
callback.andCallFake(function(status, response) {
274-
expect(response).toBe(xhrInstance.response);
272+
var xhrInstance = MockXhr.$$lastInstance;
273+
expect(xhrInstance.responseType).toBe('blob');
274+
275+
callback.andCallFake(function(status, response) {
276+
expect(response).toBe(xhrInstance.response);
277+
});
278+
279+
xhrInstance.response = {some: 'object'};
280+
xhrInstance.readyState = 4;
281+
xhrInstance.onreadystatechange();
282+
283+
expect(callback).toHaveBeenCalledOnce();
275284
});
276285

277-
xhrInstance.response = {some: 'object'};
278-
xhrInstance.readyState = 4;
279-
xhrInstance.onreadystatechange();
280286

281-
expect(callback).toHaveBeenCalledOnce();
287+
it('should read responseText if response was not defined', function() {
288+
// old browsers like IE8, don't support responseType, so they always respond with responseText
289+
290+
$backend('GET', '/whatever', null, callback, {}, null, null, 'blob');
291+
292+
var xhrInstance = MockXhr.$$lastInstance;
293+
var responseText = '{"some": "object"}';
294+
expect(xhrInstance.responseType).toBe('blob');
295+
296+
callback.andCallFake(function(status, response) {
297+
expect(response).toBe(responseText);
298+
});
299+
300+
xhrInstance.responseText = responseText;
301+
xhrInstance.readyState = 4;
302+
xhrInstance.onreadystatechange();
303+
304+
expect(callback).toHaveBeenCalledOnce();
305+
});
282306
});
283307

284308

0 commit comments

Comments
 (0)