-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
69 lines (66 loc) · 2.14 KB
/
index.html
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
64
65
66
67
68
69
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<link href="https://fonts.googleapis.com/css?family=Open+Sans:400,700&display=swap" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="./style.css" type="text/css" />
<script type="text/javascript">
/**
* Call the js request method that will retrieve cat fact data and open
* alert fill with the cat fact message (It's a demo function to
* process an onclick event)
*/
function fetchRequest(url = "", data = {}) {
const requestOptions = {
// *GET, POST, PUT, DELETE, etc.
method: "GET",
// *default, no-cache, reload, force-cache, only-if-cached
cache: "no-cache",
headers: {
"Content-Type": "application/json",
},
};
if (data.length === 0) {
// body data type must match "Content-Type" header
requestOptions.body = JSON.stringify(data);
}
fetch(url, requestOptions)
.then((response) => {
if (!response.ok) {
throw new Error(`Request error: ${response.status} - ${response.statusText}`);
}
return response.json();
})
.then((data) => {
gb.alert("Cat Fact", data.text);
})
.catch((error) => {
gb.alert(`Cat Fact Error Code ${error.name}`, error.message);
});
}
/**
* Call the gb request method that will retrieve cat fact data and open
* alert fill with the cat fact message (It's a demo function to
* process an onclick event)
*/
function buttonClick() {
// Cat Fact API Example URL
const URL_API = "https://cat-fact.herokuapp.com";
const URL_CAT_FACT_ENDPOINT = new URL("/facts/random", URL_API).toString();
// The url of the resource to load
let resourceUrl = URL_CAT_FACT_ENDPOINT;
// If method==POST, you can pass HTTP Post Params in your request
let postParams = {};
fetchRequest(resourceUrl, postParams);
}
</script>
</head>
<body>
<div class="title">
<p>Daily cat facts!</p>
<p class="cat-emoji">🐱</p>
</div>
<div class="button-fact" onclick="buttonClick()">Get a new fact</div>
</body>
</html>