Skip to content

Instantly share code, notes, and snippets.

@Jehong-Ahn
Created August 9, 2016 05:51
Show Gist options
  • Save Jehong-Ahn/744842bf7536aa290c1659279c3b1966 to your computer and use it in GitHub Desktop.
Save Jehong-Ahn/744842bf7536aa290c1659279c3b1966 to your computer and use it in GitHub Desktop.
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() {
var text = xhr.responseText;
console.log(text);
};
xhr.onerror = function() {
console.error('Woops, there was an error making the request.');
};
xhr.send();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment