fetch 使用
//最基礎長這樣
fetch('https://jsonplaceholder.typicode.com/posts')
.then((response) => response.json())
.then((json) => console.log(json));
//通常都長這樣 我應該會以這個為基礎去實作 畢竟操作性比較大
fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'POST',
body: JSON.stringify({
title: 'foo',
body: 'bar',
userId: 1,
}),
headers: {
'Content-type': 'application/json; charset=UTF-8',
},
})
.then((response) => response.json())
.then((json) => console.log(json));
參考:
https://jsonplaceholder.typicode.com/
留言
張貼留言