Fetch基于Promise的API

Time: 2024-07-21 Sunday 14:49:01
Author: Jackasher

Fetch基于Promise的API

Fetch是浏览器原生支持的Api,类似于ajax的发送请求

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
<!DOCTYPE html> 
<html>
<head>
<style>
table {
border-collapse: collapse;
width: 100;

}

td, th {
text-align: center;
border: 1px solid #000;
}
</style>
</head>
<body>
这是客户端
<button id="btn">点我加载数据</button>
<script>
const btn = document.getElementById("btn")

const root = document.documentElement
btn.onclick = function(){
fetch("http://127.0.0.1:3000/student")
.then((res ) => {
return res.json()
}).then ((res) => {
const table = document.createElement("table")
root.appendChild(table)
table.insertAdjacentHTML("beforeend","<caption>血色浪漫</caption>")
table.insertAdjacentHTML("beforeend",
`
<thead>
<tr>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>
</tr>
</thead>
`)

const tbody = document.createElement("tbody")
table.appendChild(tbody)

for (const stu of res.data) {
tbody.insertAdjacentHTML("beforeend",
`
<tr>
<td>${stu.id}</td>
<td>${stu.name}</td>
<td>${stu.age}</td>
<td>${stu.gender}</td>
<td>${stu.address}</td>
</tr>

`)
}
})
.catch((err) => {
console.log(err);
})

}




</script>
</body>
</html>

其实我们可以看到,前端渲染方式和,后端原始Servlet渲染很像,手写一大段HTML代码,然后加入数据,而且是在js里面写,那么Vue就是解决数据和视图之间的问题,双向绑定和视图渲染


Fetch基于Promise的API
http://example.com/2024/07/21/Fetch基于Promise的API/
作者
Jack Asher
发布于
2024年7月21日
许可协议