I have a problem with CORS and I do not know how to solve it.
"No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access."
My code is as follows:
window.onload = function() {
var mb = document.getElementById("myBtn");
mb.addEventListener("click", clickSomethingMatch);
}
function clickSomethingMatch() {
var createCORSRequest = function(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
// Most browsers.
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
// IE8 & IE9
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
// CORS not supported.
xhr = null;
}
return xhr;
};
var url = 'https://euw1.api.riotgames.com/lol/summoner/v3/summoners/by-name/NAMEXXX?api_key=APIKEYXXX';
var method = 'GET';
var xhr = createCORSRequest(method, url);
xhr.send();
xhr.onerror = function() {
alert('Woops, hay un error con el request');
}
xhr.onreadystatechange = function(a) {
if (xhr.readyState == 4 && xhr.status == 200) {
var response = JSON.parse(xhr.responseText);
}
document.getElementById("span").innerText = response;
};
}