Skip to content

Instantly share code, notes, and snippets.

@lindenb
Created May 20, 2014 17:32
Show Gist options
  • Save lindenb/ae6a46139a1437980ba6 to your computer and use it in GitHub Desktop.
Save lindenb/ae6a46139a1437980ba6 to your computer and use it in GitHub Desktop.
<html>
<script>
// 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() {
// All HTML5 Rocks properties support CORS.
var url = 'http://www.uniprot.org/uniprot/P02763.jsonld';
var xhr = createCORSRequest('GET', url);
if (!xhr) {
alert('CORS not supported');
return;
}
// Response handlers.
xhr.onload = function() {
var text = xhr.responseText;
var title = JSON.stringify(text);
alert('Response from CORS request to ' + url + ': ' + title);
};
xhr.onerror = function() {
alert('Woops, there was an error making the request.');
};
xhr.send();
}
</script>
<body>
<button onclick="makeCorsRequest()">X</button>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment