Skip to content

Commit 0549e04

Browse files
IgorMinarwesleycho
authored andcommitted
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 Fixed createXhr function to throw minErr when an exception occurs
1 parent d158dd1 commit 0549e04

File tree

4 files changed

+26
-17
lines changed

4 files changed

+26
-17
lines changed

src/.jshintrc

+4-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,10 @@
159159
"DIRTY_CLASS": false,
160160

161161
/* ng/directive/form.js */
162-
"nullFormCtrl": false
162+
"nullFormCtrl": false,
163+
164+
/* ng/httpBackend.js */
165+
"ActiveXObject": false
163166

164167
}
165168
}

src/ng/httpBackend.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
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+
try { return (msie <= 8 && lowercase(method) === 'patch')
6+
? new ActiveXObject('Microsoft.XMLHTTP')
7+
: new window.XMLHttpRequest();
8+
} catch (e) { throw minErr('$httpBackend')('noxhr', "This browser does not support XMLHttpRequest."); }
9+
}
1010

1111

1212
/**
@@ -28,11 +28,11 @@ var XHR = window.XMLHttpRequest || function() {
2828
*/
2929
function $HttpBackendProvider() {
3030
this.$get = ['$browser', '$window', '$document', function($browser, $window, $document) {
31-
return createHttpBackend($browser, XHR, $browser.defer, $window.angular.callbacks, $document[0]);
31+
return createHttpBackend($browser, createXhr, $browser.defer, $window.angular.callbacks, $document[0]);
3232
}];
3333
}
3434

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

3838
// TODO(vojta): fix the signature
@@ -57,7 +57,9 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument)
5757
delete callbacks[callbackId];
5858
});
5959
} else {
60-
var xhr = new XHR();
60+
61+
var xhr = createXhr(method);
62+
6163
xhr.open(method, url, true);
6264
forEach(headers, function(value, key) {
6365
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)