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 #5390

Merged
merged 2 commits into from
Jan 3, 2014
Merged
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
9 changes: 0 additions & 9 deletions docs/content/error/httpBackend/noxhr.ngdoc

This file was deleted.

24 changes: 14 additions & 10 deletions src/ng/httpBackend.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict';

var XHR = window.XMLHttpRequest || function() {
function createXhr(method) {
// IE8 doesn't support PATCH method, but the ActiveX object does
/* 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.");
};
return (msie <= 8 && lowercase(method) === 'patch')
? new ActiveXObject('Microsoft.XMLHTTP')
: new window.XMLHttpRequest();
}


/**
Expand All @@ -28,11 +28,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 +57,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 All @@ -73,7 +75,9 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument)
// onreadystatechange might by called multiple times
// with readyState === 4 on mobile webkit caused by
// xhrs that are resolved while the app is in the background (see #5426).
xhr.onreadystatechange = undefined;
//
// we must delete the property instead of setting it to undefined/null to make IE8 happy.
delete xhr.onreadystatechange;

var responseHeaders = null,
response = null;
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 @@ -250,7 +250,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 @@ -426,7 +426,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 @@ -437,7 +437,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 @@ -465,7 +465,7 @@ describe('$httpBackend', function() {

try {

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

$backend('GET', '/whatever/index.html', null, callback);
respond(0, '');
Expand All @@ -480,7 +480,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