曹耘豪的博客

浏览器使用fetch函数

  1. Get
  2. Post

https://developer.mozilla.org/zh-CN/docs/Web/API/Fetch_API/Using_Fetch

Get

1
2
3
fetch("/")
.then((response) => response.json())
.then((data) => console.log(data));

Post

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
fetch("/", { 
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: JSON.stringify({
key: 'value',
}),
credentials: "include",
})
.then(resp => resp.json())
.then(data => console.log(data))
.catch(err => {
console.error("Error:", error);
})
   /