Skip to content

Commit 4ac5227

Browse files
committed
handle APIs
1 parent 8cec86c commit 4ac5227

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

‎14_AC_APIs/app.js‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
let btn = document.querySelector("button");
2+
let p = document.querySelector("p");
3+
4+
btn.addEventListener("click", async () => {
5+
let fact = await getFacts();
6+
p.innerText = fact;
7+
});
8+
9+
let url = "https://catfact.ninja/fact";
10+
async function getFacts() {
11+
try {
12+
let res = await axios.get(url);
13+
//console.log(res.data.fact)
14+
return res.data.fact;
15+
} catch (e) {
16+
console.log("ERROR:-", e);
17+
return "No fact found";
18+
}
19+
}

‎14_AC_APIs/index.html‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Cat Facts</title>
7+
</head>
8+
<body>
9+
<h1>Get Cat Facts</h1>
10+
<button>click to get Facts</button>
11+
<p id="result"></p>
12+
13+
<script src="https://cdn.jsdelivr.net/npm/axios@1.13.2/dist/axios.min.js"></script>
14+
<script src="app.js"></script>
15+
</body>
16+
</html>

0 commit comments

Comments
 (0)