JavaScript Fetch API Cheatsheet
902
get
fetch('https://jsonplaceholder.typicode.com/todos')
.then(res => res.json())
.then(json => console.log(json))
post formdata
const formData = new FormData();
formData.append('name', 'foo');
fetch(`[url]`, { method: 'POST', body: formData})
.then(res => res.text())
.then(text => console.log(text))
post application/json
await fetch(`[url]`, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify([dataObject])
}).then(res => res.json())
.then(json => console.log(json))