Skip to content

Commit 889a9f2

Browse files
committed
fix($httpBackend): use ActiveX XHR when making PATCH requests on IE8
IE8's native XHR doesn't support PATCH requests, but the ActiveX one does. Closes angular#2518 Closes angular#5043
1 parent f3de5b6 commit 889a9f2

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

src/ng/httpBackend.js

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
'use strict';
22

3-
var XHR = window.XMLHttpRequest || function() {
4-
/* global ActiveXObject */
5-
try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch (e1) {}
6-
try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch (e2) {}
7-
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e3) {}
8-
throw minErr('$httpBackend')('noxhr', "This browser does not support XMLHttpRequest.");
9-
};
3+
function createXhr(method) {
4+
// IE8 doesn't support PATCH method, but the ActiveX object does
5+
return (msie <= 8 && lowercase(method) === 'patch')
6+
? new ActiveXObject('Microsoft.XMLHTTP')
7+
: new window.XMLHttpRequest;
8+
}
109

1110

1211
/**
@@ -28,11 +27,11 @@ var XHR = window.XMLHttpRequest || function() {
2827
*/
2928
function $HttpBackendProvider() {
3029
this.$get = ['$browser', '$window', '$document', function($browser, $window, $document) {
31-
return createHttpBackend($browser, XHR, $browser.defer, $window.angular.callbacks, $document[0]);
30+
return createHttpBackend($browser, createXhr, $browser.defer, $window.angular.callbacks, $document[0]);
3231
}];
3332
}
3433

35-
function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument) {
34+
function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDocument) {
3635
var ABORTED = -1;
3736

3837
// TODO(vojta): fix the signature
@@ -57,7 +56,9 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument)
5756
delete callbacks[callbackId];
5857
});
5958
} else {
60-
var xhr = new XHR();
59+
60+
var xhr = createXhr(method);
61+
6162
xhr.open(method, url, true);
6263
forEach(headers, function(value, key) {
6364
if (isDefined(value)) {

src/ngMock/angular-mocks.js

+4
Original file line numberDiff line numberDiff line change
@@ -1572,6 +1572,10 @@ function MockHttpExpectation(method, url, data, headers) {
15721572
};
15731573
}
15741574

1575+
function createMockXhr() {
1576+
return new MockXhr();
1577+
}
1578+
15751579
function MockXhr() {
15761580

15771581
// hack for testing $http, $httpBackend

test/ng/httpBackendSpec.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ describe('$httpBackend', function() {
5353
})
5454
}
5555
};
56-
$backend = createHttpBackend($browser, MockXhr, fakeTimeout, callbacks, fakeDocument);
56+
$backend = createHttpBackend($browser, createMockXhr, fakeTimeout, callbacks, fakeDocument);
5757
callback = jasmine.createSpy('done');
5858
}));
5959

@@ -238,7 +238,7 @@ describe('$httpBackend', function() {
238238
expect(response).toBe('response');
239239
});
240240

241-
$backend = createHttpBackend($browser, SyncXhr);
241+
$backend = createHttpBackend($browser, function() { return new SyncXhr() });
242242
$backend('GET', '/url', null, callback);
243243
expect(callback).toHaveBeenCalledOnce();
244244
});
@@ -414,7 +414,7 @@ describe('$httpBackend', function() {
414414

415415

416416
it('should convert 0 to 200 if content', function() {
417-
$backend = createHttpBackend($browser, MockXhr);
417+
$backend = createHttpBackend($browser, createMockXhr);
418418

419419
$backend('GET', 'file:///whatever/index.html', null, callback);
420420
respond(0, 'SOME CONTENT');
@@ -425,7 +425,7 @@ describe('$httpBackend', function() {
425425

426426

427427
it('should convert 0 to 404 if no content', function() {
428-
$backend = createHttpBackend($browser, MockXhr);
428+
$backend = createHttpBackend($browser, createMockXhr);
429429

430430
$backend('GET', 'file:///whatever/index.html', null, callback);
431431
respond(0, '');
@@ -453,7 +453,7 @@ describe('$httpBackend', function() {
453453

454454
try {
455455

456-
$backend = createHttpBackend($browser, MockXhr);
456+
$backend = createHttpBackend($browser, createMockXhr);
457457

458458
$backend('GET', '/whatever/index.html', null, callback);
459459
respond(0, '');
@@ -468,7 +468,7 @@ describe('$httpBackend', function() {
468468

469469

470470
it('should return original backend status code if different from 0', function () {
471-
$backend = createHttpBackend($browser, MockXhr);
471+
$backend = createHttpBackend($browser, createMockXhr);
472472

473473
// request to http://
474474
$backend('POST', 'http://rest_api/create_whatever', null, callback);

0 commit comments

Comments
 (0)