Ajax request error on Node

Asked

Viewed 41 times

1

I have a view that contains a login form. The request is sent via ajax, but the data is not being captured and req.body in the back end is empty. Even if I create a string and switch to Node, req.body remains 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);
});

Route in the Node:

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'});
	}
});

  • What appears in console.log on the Node side ? And what appears on the console.log html-side ?

  • On the Node side it appears: { } and on the html side it appears { } also @Isac

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.