-1
const form = document.getElementById('form')
form.addEventListener('submit', function (e) {
e.preventDefault();
const firsName = document.getElementById('firstname')
const lastName = document.getElementById('lastName')
const email = document.getElementById('email')
const age = document.getElementById('age')
const API_URL = 'http://localhost:3000/users';
fetch(API_URL, {
method: 'POST',
body: JSON.stringify({
firsName,
lastName,
email,
age
}),
headers: {
"Content-Type": "application/json; charset=UTF-8"
}
}).then((response) =>response.json())
.then((data) => {
console.log(data)
})
})
The function
fetch
does not re-load the page, has some other reason for this, most likely the event ofsubmit
is not being prevented, but only with what you posted can not help. The id of your form isform
even?– Andre