JSFiddle - React, Tailwind, and code Playground
check loteria de navidad api El Pais: CORS problem
by abubelinha
HTML
CORS problem.
<p>We would need a proxy.php in the same domain as this javascript request.
<p>proxy.php would get the js request, download the file from the remote server, and echo it so it is returned from the same domain the js request was sent from.
<p>
Another try <a target=_blank href=http://jsfiddle.net/abubelinha/Lne8ytu9/>here</a>, using jquery.getJSON() / jquery.get()
</p>
JavaScript
var url='https://api.elpais.com/ws/LoteriaNavidadPremiados?n=12347';
url+= "&callback=?";
var jsonFile = new XMLHttpRequest();
jsonFile.open("GET",url,true);
jsonFile.send();
jsonFile.onreadystatechange = function() {
if (jsonFile.readyState== 4 && jsonFile.status == 200) {
console.log( jsonFile.responseText);
document.write(jsonFile.responseText);
}
}
/*
// 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;
}
// Helper method to parse the title tag from the response.
function getTitle(text) {
return text.match('<title>(.*)?</title>')[1];
}
// Make the actual CORS request.
function makeCorsRequest() {
// This is a sample server that supports CORS.
//var url = 'http://html5rocks-cors.s3-website-us-east-1.amazonaws.com/index.html';
var xhr = createCORSRequest('GET', url);
if (!xhr) {
alert('CORS 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('Woops, there was an error making the request.');
};
xhr.send();
}
makeCorsRequest();
*/