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

Commit f227e4d

Browse files
committed
refactor($http) Simplify code by removing the createXhr() method
This removes a workaround for IE 8 and and error handling for IE6.
1 parent a8fe2cc commit f227e4d

File tree

4 files changed

+12
-38
lines changed

4 files changed

+12
-38
lines changed

docs/content/error/$httpBackend/noxhr.ngdoc

-10
This file was deleted.

src/ng/httpBackend.js

+4-16
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
11
'use strict';
22

3-
function createXhr(method) {
4-
//if IE and the method is not RFC2616 compliant, or if XMLHttpRequest
5-
//is not available, try getting an ActiveXObject. Otherwise, use XMLHttpRequest
6-
//if it is available
7-
if (msie <= 8 && (!method.match(/^(get|post|head|put|delete|options)$/i) ||
8-
!window.XMLHttpRequest)) {
9-
return new window.ActiveXObject("Microsoft.XMLHTTP");
10-
} else if (window.XMLHttpRequest) {
11-
return new window.XMLHttpRequest();
12-
}
13-
14-
throw minErr('$httpBackend')('noxhr', "This browser does not support XMLHttpRequest.");
15-
}
3+
var XHR = window.XMLHttpRequest;
164

175
/**
186
* @ngdoc service
@@ -32,11 +20,11 @@ function createXhr(method) {
3220
*/
3321
function $HttpBackendProvider() {
3422
this.$get = ['$browser', '$window', '$document', function($browser, $window, $document) {
35-
return createHttpBackend($browser, createXhr, $browser.defer, $window.angular.callbacks, $document[0]);
23+
return createHttpBackend($browser, XHR, $browser.defer, $window.angular.callbacks, $document[0]);
3624
}];
3725
}
3826

39-
function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDocument) {
27+
function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument) {
4028
var ABORTED = -1;
4129

4230
// TODO(vojta): fix the signature
@@ -59,7 +47,7 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc
5947
});
6048
} else {
6149

62-
var xhr = createXhr(method);
50+
var xhr = new XHR;
6351

6452
xhr.open(method, url, true);
6553
forEach(headers, function(value, key) {

src/ngMock/angular-mocks.js

-4
Original file line numberDiff line numberDiff line change
@@ -1620,10 +1620,6 @@ function MockHttpExpectation(method, url, data, headers) {
16201620
};
16211621
}
16221622

1623-
function createMockXhr() {
1624-
return new MockXhr();
1625-
}
1626-
16271623
function MockXhr() {
16281624

16291625
// hack for testing $http, $httpBackend

test/ng/httpBackendSpec.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global createHttpBackend: false, createMockXhr: false, MockXhr: false */
1+
/* global createHttpBackend: false, MockXhr: false */
22
'use strict';
33

44
describe('$httpBackend', function() {
@@ -57,7 +57,7 @@ describe('$httpBackend', function() {
5757
})
5858
}
5959
};
60-
$backend = createHttpBackend($browser, createMockXhr, fakeTimeout, callbacks, fakeDocument);
60+
$backend = createHttpBackend($browser, MockXhr, fakeTimeout, callbacks, fakeDocument);
6161
callback = jasmine.createSpy('done');
6262
}));
6363

@@ -442,7 +442,7 @@ describe('$httpBackend', function() {
442442

443443

444444
it('should convert 0 to 200 if content and file protocol', function() {
445-
$backend = createHttpBackend($browser, createMockXhr);
445+
$backend = createHttpBackend($browser, MockXhr);
446446

447447
$backend('GET', 'file:///whatever/index.html', null, callback);
448448
respond(0, 'SOME CONTENT');
@@ -452,7 +452,7 @@ describe('$httpBackend', function() {
452452
});
453453

454454
it('should convert 0 to 200 if content for protocols other than file', function() {
455-
$backend = createHttpBackend($browser, createMockXhr);
455+
$backend = createHttpBackend($browser, MockXhr);
456456

457457
$backend('GET', 'someProtocol:///whatever/index.html', null, callback);
458458
respond(0, 'SOME CONTENT');
@@ -462,7 +462,7 @@ describe('$httpBackend', function() {
462462
});
463463

464464
it('should convert 0 to 404 if no content and file protocol', function() {
465-
$backend = createHttpBackend($browser, createMockXhr);
465+
$backend = createHttpBackend($browser, MockXhr);
466466

467467
$backend('GET', 'file:///whatever/index.html', null, callback);
468468
respond(0, '');
@@ -472,7 +472,7 @@ describe('$httpBackend', function() {
472472
});
473473

474474
it('should not convert 0 to 404 if no content for protocols other than file', function() {
475-
$backend = createHttpBackend($browser, createMockXhr);
475+
$backend = createHttpBackend($browser, MockXhr);
476476

477477
$backend('GET', 'someProtocol:///whatever/index.html', null, callback);
478478
respond(0, '');
@@ -500,7 +500,7 @@ describe('$httpBackend', function() {
500500

501501
try {
502502

503-
$backend = createHttpBackend($browser, createMockXhr);
503+
$backend = createHttpBackend($browser, MockXhr);
504504

505505
$backend('GET', '/whatever/index.html', null, callback);
506506
respond(0, '');
@@ -515,7 +515,7 @@ describe('$httpBackend', function() {
515515

516516

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

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

0 commit comments

Comments
 (0)