submit formdata in fetch
// Build formData object.
let formData = new FormData();
formData.append('name', 'John');
formData.append('password', 'John123');
fetch("api/SampleData",
{
body: formData,
method: "post"
});
submit formdata in fetch
// Build formData object.
let formData = new FormData();
formData.append('name', 'John');
formData.append('password', 'John123');
fetch("api/SampleData",
{
body: formData,
method: "post"
});
fetch suntax
fetch('http://example.com/movies.json')
.then((response) => {
return response.json();
})
.then((data) => {
console.log(data);
});
fetch post form data
// It's almost copy paste ajax function,
// just select the right Form and set the right URL
document.getElementById('submitBtn').onclick = ajax
function ajax(e){
e.preventDefault()
// Get form element and create a formData object
const form = document.getElementsByTagName('form')[0]
const formData = {}
// Get all input elements inside a form
// and create key:value pairs inside formData
form.querySelectorAll('input').forEach(element => {
formData[element.name] = element.value
})
// Send data to your backend
fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(formData)
})
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us