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

feat($http): CORS requests support for IE9 #11355

Closed
wants to merge 1 commit into from
Closed
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
71 changes: 53 additions & 18 deletions src/ng/httpBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ function createXhr() {
return new window.XMLHttpRequest();
}

function createXdr() {
return new window.XDomainRequest();
}

/**
* @ngdoc service
* @name $httpBackend
Expand All @@ -22,11 +26,11 @@ function createXhr() {
*/
function $HttpBackendProvider() {
this.$get = ['$browser', '$window', '$document', function($browser, $window, $document) {
return createHttpBackend($browser, createXhr, $browser.defer, $window.angular.callbacks, $document[0]);
return createHttpBackend($browser, createXhr, $browser.defer, $window.angular.callbacks, $document[0], createXdr);
}];
}

function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDocument) {
function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDocument, createXdr) {
// TODO(vojta): fix the signature
return function(method, url, post, callback, headers, timeout, withCredentials, responseType) {
$browser.$$incOutstandingRequestCount();
Expand All @@ -46,16 +50,56 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc
});
} else {

var xhr = createXhr();
var xhr;
var isXdr = false;
// only if we are in IE9
if (createXdr && window.XDomainRequest) {
// and it's CORS
var hostname = window.location.hostname + (window.location.port ? ':' + window.location.port : '');
if (!(!/^https?:\/\/([^\?\/]+)/.test(url) || RegExp.$1 === hostname)) {
isXdr = true;
}
}
if (!isXdr) {
xhr = createXhr();
} else {
xhr = createXdr();
// these methods MUST be defined (and before xhr.open), otherwise requests will fail in IE9
xhr.onprogress = function() {};
xhr.ontimeout = function() {};
if (method !== 'GET') {
method = 'POST';
}
xhr.getAllResponseHeaders = function() {
return '';
};
}

var requestError = function() {
// The response is always empty
// See https://xhr.spec.whatwg.org/#request-error-steps and https://fetch.spec.whatwg.org/#concept-network-error
completeRequest(callback, -1, null, null, '');
};

xhr.onerror = requestError;
if (!isXdr) {
xhr.onabort = requestError;
}

xhr.open(method, url, true);
forEach(headers, function(value, key) {
if (isDefined(value)) {
xhr.setRequestHeader(key, value);
}
});
if (!isXdr) {
forEach(headers, function(value, key) {
if (isDefined(value)) {
xhr.setRequestHeader(key, value);
}
});
}

xhr.onload = function requestLoaded() {
if (isXdr) {
xhr.status = 200;
xhr.statusText = 'OK';
}
var statusText = xhr.statusText || '';

// responseText is the old-school way of retrieving response (supported by IE8 & 9)
Expand All @@ -79,16 +123,7 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc
statusText);
};

var requestError = function() {
// The response is always empty
// See https://xhr.spec.whatwg.org/#request-error-steps and https://fetch.spec.whatwg.org/#concept-network-error
completeRequest(callback, -1, null, null, '');
};

xhr.onerror = requestError;
xhr.onabort = requestError;

if (withCredentials) {
if (withCredentials && !isXdr) {
xhr.withCredentials = true;
}

Expand Down