4.1 語法
Last updated
Last updated
fetch("API 網址").then((res) => {
if(res.ok){
return res.json();
}
}).then((data) => {
console.log(data); // 後端回傳的資料,這裡取得
});fetch("API 網址").then(res => res.json()).then(data => {
console.log(data);
});fetch("API 網址", {
method: "POST", // POST | PUT | PATCH | DELETE
body: 資料 // 欲傳送的資料
}).then // 這裡省略不寫,同上述 GET 的部份。function test_func(){
fetch("API 網址").then(res => res.json()).then(data => {
console.log(data);
});
}async function test_func(){
let res = await fetch("API 網址"); // 若是非 GET,再加第二個參數
let data = await res.json();
}