Skip to content

Instantly share code, notes, and snippets.

@pteacher
Created December 7, 2020 05:56
Show Gist options
  • Save pteacher/bea1f47a42aafb966b972a28df36c037 to your computer and use it in GitHub Desktop.
Save pteacher/bea1f47a42aafb966b972a28df36c037 to your computer and use it in GitHub Desktop.
API Example for COM19
<!DOCTYPE html>
<html>
<head>
<title>API Example</title>
<script type="text/javascript">
function get_dog() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
console.log(this.status);
if (this.readyState == 4 && this.status == 200) {
var dogimg = JSON.parse(this.responseText);
document.getElementById('dog').src = dogimg.message;
}
};
xhttp.open("GET", "https://dog.ceo/dsf", true);
xhttp.send();
}
get_dog();
</script>
</head>
<body>
<div>
<img src="" id="dog" onclick="get_dog()" alt="press on image to load new dog">
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>API Example</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript">
function get_dog() {
$.get("https://dog.ceo/api/breeds/image/random", function(data, status){
console.log(data);
$("#dog").attr("src", data.message);
});
}
get_dog();
</script>
</head>
<body>
<div>
<img src="" id="dog" onclick="get_dog()" alt="press on image to load new dog">
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment