1let data = {element: "barium"};
2
3fetch("/post/data/here", {
4 method: "POST",
5 body: JSON.stringify(data)
6}).then(res => {
7 console.log("Request complete! response:", res);
8});
9
10
11// If you are as lazy as me (or just prefer a shortcut/helper):
12
13window.post = function(url, data) {
14 return fetch(url, {method: "POST", body: JSON.stringify(data)});
15}
16
17// ...
18
19post("post/data/here", {element: "osmium"});
20
1const postData = async ( url = '', data = {})=>{
2 console.log(data);
3 const response = await fetch(url, {
4 method: 'POST',
5 credentials: 'same-origin',
6 headers: {
7 'Content-Type': 'application/json',
8 },
9 // Body data type must match "Content-Type" header
10 body: JSON.stringify(data),
11 });
12
13 try {
14 const newData = await response.json();
15 console.log(newData);
16 return newData;
17 }catch(error) {
18 console.log("error", error);
19 }
20 }
21