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

fix($httpBackend): use ActiveX XHR when making PATCH requests on IE8 #5579

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions src/ng/httpBackend.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
'use strict';

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


/**
Expand All @@ -28,11 +29,11 @@ var XHR = window.XMLHttpRequest || function() {
*/
function $HttpBackendProvider() {
this.$get = ['$browser', '$window', '$document', function($browser, $window, $document) {
return createHttpBackend($browser, XHR, $browser.defer, $window.angular.callbacks, $document[0]);
return createHttpBackend($browser, createXhr, $browser.defer, $window.angular.callbacks, $document[0]);
}];
}

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

// TODO(vojta): fix the signature
Expand All @@ -57,7 +58,9 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument)
delete callbacks[callbackId];
});
} else {
var xhr = new XHR();

var xhr = createXhr(method);

xhr.open(method, url, true);
forEach(headers, function(value, key) {
if (isDefined(value)) {
Expand Down
4 changes: 4 additions & 0 deletions src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -1572,6 +1572,10 @@ function MockHttpExpectation(method, url, data, headers) {
};
}

function createMockXhr() {
return new MockXhr();
}

function MockXhr() {

// hack for testing $http, $httpBackend
Expand Down
12 changes: 6 additions & 6 deletions test/ng/httpBackendSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('$httpBackend', function() {
})
}
};
$backend = createHttpBackend($browser, MockXhr, fakeTimeout, callbacks, fakeDocument);
$backend = createHttpBackend($browser, createMockXhr, fakeTimeout, callbacks, fakeDocument);
callback = jasmine.createSpy('done');
}));

Expand Down Expand Up @@ -238,7 +238,7 @@ describe('$httpBackend', function() {
expect(response).toBe('response');
});

$backend = createHttpBackend($browser, SyncXhr);
$backend = createHttpBackend($browser, function() { return new SyncXhr() });
$backend('GET', '/url', null, callback);
expect(callback).toHaveBeenCalledOnce();
});
Expand Down Expand Up @@ -414,7 +414,7 @@ describe('$httpBackend', function() {


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

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


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

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

try {

$backend = createHttpBackend($browser, MockXhr);
$backend = createHttpBackend($browser, createMockXhr);

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


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

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