cors proxy
JavaScript
// 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(uri) {
console.log("got uri: " + uri);
var xhr = createCORSRequest('GET', uri);
if (!xhr) {
alert('CORS not supported');
return;
}
// Response handlers.
xhr.onload = function() {
console.log('Response from CORS request to ' + uri + ': ' + xhr.responseText);
};
xhr.onerror = function() {
console.log('Woops, there was an error making the request to ' + uri + '.');
};
xhr.send();
}
$(document).ready(function() {
makeCorsRequest('https://dak.sh/json/sorted.json');
});