Saturday, 20 May 2017

HOW TO: send and get "formdata" with pug and javascript

got this form in pug:

extends layout

block login
    form(id="form-login")
        input(type="text", name="email", value="", placeholder="Tu email")
        br
        input(type="password", name="password", value="", placeholder="Tu contraseƱa")
        br
        input(type="submit" value="Conectar")

I'm trying to handle it with this script right after above code (properly idented):

script.
    const formLogin = document.querySelector('#form-login');

formLogin.addEventListener('submit', function(event) {
    const formData = new FormData(formLogin);
    console.log('Contenido de Formulario: ', formData);
    event.preventDefault();
    fetch('/signin', {
        method: 'POST',
        //body: formData
    })
    //- .then(function(res) {
    //-     res.json();
    //- })
    .then(function(data) {
        //console.log(data)
        localStorage.setItem('token', data.token)
    })
});

Handler function is receiving an empty req.body.

This question extendes my last question I hope someone knows what's happening here, thanks in advance.



via Adolfo Onrubia

No comments:

Post a Comment