U
U
UNy2018-05-22 21:28:18
HTML
UNy, 2018-05-22 21:28:18

Submitting a form to the server?

How to send and receive a simple form correctly:

<form name="test" method="post" action="/comment">
       <p><b>Ваше имя:</b><br>
       <input type="text" size="40">
       </p>
      <input type="submit" value="Отправить">
</form>

How to accept such a form on the server?
app.post('/comment',jsonParser,function (req,res,next) {
    console.log(req.body.email)
});

And why does it load another page after submitting?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Shahob I, 2018-05-22
@UNy

You need to remove the jsonParser middleware and return a response

app.post('/comment',function (req,res,next) {
    console.log(req.body.email);
     res.send('Hello World!');
});

And read expressjs.com/ru/guide/using-middleware.html

M
msdos-x86, 2018-05-23
@msdos-x86

so that after sending it does not reload the page, you need to put an onSubmit handler and write event.preventDefault()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question