-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
28 lines (24 loc) · 815 Bytes
/
index.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
function getAdvice()
{
const adviceIDElement = document.getElementById("advice-id");
const adviceElement = document.getElementById("advice");
const apiAdress = "https://api.adviceslip.com/advice";
fetch(apiAdress)
.then(response => {
if (response.ok) {
return response.json(); // Parse the response data as JSON
} else {
throw new Error('API request failed');
}
})
.then(data => {
// Process the response data here
console.log(data); // Example: Logging the data to the console
adviceIDElement.innerText = data.slip.id;
adviceElement.innerText = data.slip.advice;
})
.catch(error => {
// Handle any errors here
console.error(error); // Example: Logging the error to the console
});
}