-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.js
63 lines (50 loc) · 2 KB
/
code.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Purpose: as soon as the page is loaaded, the protocols
// for requesting an API, openning it, and sending it
// are performed
// Notes: - error cases were also included
function load_animal_API() {
// make instance of request object
// to make HTTP request after tha page is loaded
request = new XMLHttpRequest();
// if request is unsuccesful, an alert is sent
if (!request) {
{alert("Unable to create HTTPRequest object"); return; }
};
// set the URL for the AJAX request to be the JSON file API
request.open("GET", "https://zoo-animal-api.herokuapp.com/animals/rand", true);
// set up event handler/callback
request.onreadystatechange = function() {
// check for success
if (this.readyState == 4 && this.status == 200) {
document.getElementById("greetings").innerHTML =
"Thank you for taking a look in our beloved animals!";
} else {
document.getElementById("greetings").innerHTML =
"Error on accessing API. Reload the page";
}
};
// send the request
request.send();
}
// Purpose: retrieve elements from the API database, fetching elements
// Notes: - delegate the action to a helper function (display_animal)
// - event function -> is activated once the button on HTML is clicked
function output_animal() {
fetch("https://zoo-animal-api.herokuapp.com/animals/rand")
.then(response => response.json())
.then(json => display_animal(json))
.catch(error => console.log(error));
}
// Purpose: print on specific HTML divs data fetched from the JSON API
function display_animal(struct) {
$("#animal").html( "<br>" +
struct["name"])
$("#animal-origin").html(
"Now that this " + struct["name"] + " has been rescued," +
" we will run a careful readaptation process so it can" +
" be prepared to live in any of its natural habitats:<br><br> "
+ struct["habitat"] + "<br><br>")
$("#animal-image").html(
"<img src=\""
+ struct["image_link"] + "\" width='200px' /> <br><br>")
}