Sorry if this seems repetitive, but I am unable to find a definitive answer to similar questions.
Whenever I attempt to make a CORS request for XML, I consistently encounter an "Access is denied" JavaScript error in IE8.
The code I am using is based on the following example:
// Setting up the XHR object.
function createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
// Using XHR for Chrome/Firefox/Opera/Safari.
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
// Implementing XDomainRequest for IE.
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
// CORS not supported.
xhr = null;
}
return xhr;
}
// Helper function to extract the title tag from the response.
function getTitle(text) {
return text.match('<title>(.*)?</title>')[1];
}
// Executing the CORS request.
function makeCorsRequest() {
// All properties of HTML5 Rocks support CORS.
var url = 'http://updates.html5rocks.com';
var xhr = createCORSRequest('GET', url);
if (!xhr) {
alert('CORS is not supported');
return;
}
// Response handlers.
xhr.onload = function() {
var text = xhr.responseText;
var title = getTitle(text);
alert('Response from CORS request to ' + url + ': ' + title);
};
xhr.onerror = function() {
alert('Oops, an error occurred while processing the request.');
};
xhr.send();
}
sourced from http://www.html5rocks.com/en/tutorials/cors/
This should function properly in IE8 with XDomainRequest. When I test the sample on the html5rocks page by clicking "Run sample", it works fine in IE8. However, when I use the same code on my own page, I encounter the "Access is denied" error at the xhr.open() line within XDomainRequest.
This issue has me puzzled - the server configuration is correct, so it must be something related to the frontend. Any help would be greatly appreciated. Thank you!