I have a view that contains a form for login. The request is sent via ajax, but the data is not being fetched and the req.body in the backend is empty. Even though I'm creating a string and passing to the node, the req.body is still empty.
Form contained in view:
<form method="POST" id="login">
<div class="form-group">
<input id="email" type="text" name="email" required="" placeholder="E-mail" class="input-material">
</div>
<div class="form-group">
<input id="password" type="password" name="password" required="" class="input-material" placeholder="Senha">
</div>
<button type="submit" class="btn btn-primary">Login</button>
</form>
Ajax request:
function autenticar(formData){
$.ajax({
url: '/login',
type: 'POST',
data: formData,
contentType: false,
processData: false,
}).done( data => {
console.log(data);
});
}
$('#login').on('submit', function (event) {
event.preventDefault();
let form = document.getElementById('login');
let formData = new FormData(form);
autenticar(formData);
});
Node route:
router.post('/login', async (req, res) => {
try{
console.log(req.body);
const { email, password } = req.body;
res.send({email, password});
}
catch (err){
return res.status(400).send({error: 'Erro ao processar o login'});
}
});