js fetch 'post' json
//Obj of data to send in future like a dummyDb const data = { username: 'example' }; //POST request with body equal on data in JSON format fetch('https://example.com/profile', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(data), }) .then((response) => response.json()) //Then with the data from the response in JSON... .then((data) => { console.log('Success:', data); }) //Then with the error genereted... .catch((error) => { console.error('Error:', error); }); // Yeah
Source: developer.mozilla.org
javascript fetch post form data
javascript fetch post form data with headers , in reactjs -------------------------------- let formData = new FormData(); formData.append('data', 'formdata'); formData.append('data', 'formdata'); export const getData = async () => { await fetch('----url------', { method: 'POST', headers: { Authorization: 'Basic -------token----------', }, body: formData }).then(response => response.json()) .then(data => { console.log(data); }) }
Source: ashabbsoftmart.web.app
sending form data with fetch using js
const data = { username: 'example' }; fetch('https://example.com/profile', { method: 'POST', // or 'PUT' headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(data), }) .then(response => response.json()) .then(data => { console.log('Success:', data); }) .catch((error) => { console.error('Error:', error); });
Source: developer.mozilla.org
Send Form Data Using Fetch
async function sendFormData() { const myData = document.getElementById("myData"); const formData = new FormData(myData); const obj= Object.fromEntries(formData); const res = await fetch('/test', {method:'POST', body: JSON.stringify(obj), headers:{'Content-type': 'application/json; charset=UTF-8'}}); const response = await res.json(); console.log(response["a"]); }
Javascript Fetch FormData
fetch(form.action, {method:'post', body: new FormData(form)});
Source: stackoverflow.com