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

Commit

Permalink
fix($httpBackend): use ActiveX XHR when making PATCH requests on IE8
Browse files Browse the repository at this point in the history
IE8's native XHR doesn't support PATCH requests, but the ActiveX one does.

I'm also removing the noxhr error doc because nobody will ever get that error.

Closes #2518
Closes #5043
  • Loading branch information
IgorMinar committed Jan 3, 2014
1 parent 6a6f71f commit 6c17d02
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 24 deletions.
9 changes: 0 additions & 9 deletions docs/content/error/httpBackend/noxhr.ngdoc

This file was deleted.

20 changes: 11 additions & 9 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 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

6 comments on commit 6c17d02

@cgrodriguez
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@IgorMinar I have a very simple routing example that is working on angular 1.2.6 and isn't working on 1.2.7 (just ie8, working on firefox and chrome). Is it possible that this commit is breaking routing in ie8? When I replace this code on 1.2.7 (and 1.2.8) with the previous code on 1.2.6 it works again. Here is a plunkr with the example I was talking about: http://plnkr.co/edit/Tpceyo689yEC8WRHiMG2?p=preview
Also I don't know if this is the right place to submit this, should I open an issue with the problem (I'm not very familiar with github, sorry)?

@caitp
Copy link
Contributor

@caitp caitp commented on 6c17d02 Jan 14, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cgrodriguez that plnkr seems to work fine in ie8

@cgrodriguez
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it? Not for me, I get Typeerrors in ie8 console when clicking on links and views aren't shown. Same thing is happening in other projects I updated angular to 1.2.7 or 1.2.8. When debugging it, createXhr function is throwing the TypeError exception I talked about :(

@caitp
Copy link
Contributor

@caitp caitp commented on 6c17d02 Jan 14, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on saucelabs I'm not seeing any problems in the webconsole with the default browser settings. Maybe you have a security policy or something causing the issue?

@cgrodriguez
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought about that and tried a bit without success. I'll give it another try asap and keep you updated. Thanks for your time and help!

@cgrodriguez
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was indeed a security issue, I had the "Enable Native xmlHTTP support" disabled so the "new window.XMLHttpRequest()" line threw an exception. I'll see if I can work around this since I can't tweak security settings for users at work. Thanks again! :)

Please sign in to comment.