Skip to content

Instantly share code, notes, and snippets.

@baskaufs
Created May 31, 2015 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baskaufs/5407b796046ebf6437ad to your computer and use it in GitHub Desktop.
Save baskaufs/5407b796046ebf6437ad to your computer and use it in GitHub Desktop.
test using Javascript to request a cross-domain HTTP GET
<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() {//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
}
http.send(null);
</script>
</body>
</html>
@baskaufs
Copy link
Author

  1. To run this script on your local machine, save the javascript-http-get.htm file to your hard drive and open it as a local file using the Firefox browser. I think most of the other browsers won't run the Javascript unless the file is coming through the Web from a server. Each time you reload the page, the GET request is repeated.
  2. The URL that is set by default is a call the the GBIF Web API and it requests JSON data about a particular occurrence. There is no problem receiving the response text, which will display in an alert popup.
  3. To try sending the SPARQL query, comment out the line with the GBIF URL and remove the "//" from the beginning of the next line with the phylodiversity.net URL. If you save the page and reload it, nothing happens. I changed the alert to show me the http.status value and I got "0" which is not a valid HTTP status code. So there is some kind of error. I didn't take the exercise further than that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment