/
axios-Sample.html
77 lines (69 loc) · 1.94 KB
/
axios-Sample.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
70
71
72
73
74
75
76
77
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
<style>
table {
border-spacing: 0;
}
table tr th {
border-bottom: 2px solid black;
padding: 5px;
}
table tr td {
border-bottom: 1px solid black;
padding: 5px;
}
</style>
</head>
<body>
<input type="text" id="user_id" />
<button type="button" onclick="loadArticles()">
Load Articles from Server
</button>
<table id="list" width="100%">
<thead>
<tr>
<th>Title</th>
<th>comment Count</th>
</tr>
</thead>
<tbody></tbody>
</table>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
function loadComments(article) {
axios
.get(
"https://jsonplaceholder.typicode.com/comments?postId=" + article.id
)
.then((response) => {
const table = document.getElementById("list");
const tr = document.createElement("tr");
const titleTd = document.createElement("td");
const commentCountTd = document.createElement("td");
titleTd.innerText = article.title;
commentCountTd.innerText = response.data.length;
commentCountTd.style.textAlign = "center";
tr.appendChild(titleTd);
tr.appendChild(commentCountTd);
table.appendChild(tr);
});
}
function loadArticles() {
const userId = document.getElementById("user_id").value;
axios
.get("https://jsonplaceholder.typicode.com/posts?userId=" + userId)
.then((response) => {
for (let i = 0; i < response.data.length; i++) {
loadComments(response.data[i]);
}
});
}
</script>
</body>
</html>