I'm trying to get the content of various .js files from a code that runs on the browser (through plugin).
I have no problem getting .js files that reside on the same server, using XMLHttpRequest
but when I need to fetch the content of .js files that are on other domains I'm trying to use CORS but I can't seem to get it to work
This is my code
// Create the XHR object.
function createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
// XHR for Chrome/Firefox/Opera/Safari.
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
// XDomainRequest for IE.
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
// CORS not supported.
xhr = null;
}
return xhr;
}
// Make the actual CORS request.
function makeCorsRequest(url) {
var xhr = createCORSRequest('GET', url);
if (!xhr) {
alert('CORS not supported');
return;
}
// Response handlers.
xhr.onload = function() {
return xhr.responseText;
};
xhr.onerror = function() {
alert('There was an error making the request.');
};
xhr.send();
}
When I run:
url = `https://......./javascript.js'
res = makeCORSTRequest(url)
I get the 'There was an error making the request.' error alert, and the console logs
XMLHttpRequest cannot load https://......./javascript.js. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://.....com' is therefore not allowed access.