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);
})
}
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"]);
}
fetch(form.action, {method:'post', body: new FormData(form)});