Skip to content

Instantly share code, notes, and snippets.

@baskaufs
Created June 1, 2015 12:19
Show Gist options
  • Save baskaufs/f3eca39d586a2d35ec11 to your computer and use it in GitHub Desktop.
Save baskaufs/f3eca39d586a2d35ec11 to your computer and use it in GitHub Desktop.
modified ajax with error handler
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>test HTTP GET from cross-domain URL using Javascript</title>
</head>
<body>
This will send an HTTP GET request to a URL.
<script type="text/javascript">
var http = new XMLHttpRequest();
var url = "http://api.gbif.org/v1/occurrence/search?occurrenceID=http%3A%2F%2Fbioimages.vanderbilt.edu%2Find-baskauf%2F28747%232003-07-25";
//var url = "http://tdwg-rdf.phylodiversity.net/store3/sparql?query=SELECT%20DISTINCT%20%3Fimage%20WHERE%20%7B%3Chttp%3A%2F%2Fbioimages.vanderbilt.edu%2Find-baskauf%2F00000%3E%20%3Chttp%3A%2F%2Fxmlns.com%2Ffoaf%2F0.1%2Fdepiction%3E%20%3Fimage.%7D";
http.open("GET", url, true);
http.onreadystatechange = function() {
if (http.readyState == 4) //Request is complete
{
if (http.status == 200) //Response is ready
{
alert(http.responseText);
}
else //Error handler
{
alert("Request failed with error code: " + http.status);
}
}
}
http.send(null);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment