Send the form to server Node.js



  • I'm sending a form to serve Node, and after the shipment, there's an empty page in the browser, there's a URL shaper at the end, not a page with which I'm sending out the form, so the empty page is downloaded.

    How do we make sure that after sending the form, there's a page with which I'm sending the form, that's a contact page, not an empty page?

    Example of server code:

    app.use('/handler', bodyParser.urlencoded({
        extended: true
    }));
    

    app.post('/handler', function(req, res, next) {
    // это обработчик на сервере, после отправки формы url заканчивается на "/handler"
    console.log(req.body);
    res.end();
    });

    HTML marking:

    <form action="/handler" method="POST" id="myForm">
    <!-- Набор полей формы -->
    </form>



  • Example of template sabotage by XHR:

    <script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
    <form id="myForm">
      Name:<br>
      <input type="text" name="name" value=""><br>
      <br>
      Comment:<br>
      <textarea name="comment"></textarea><br>
      <input type="submit" value="Send">
    </form>
    <script>
    $(function () {
      $("#myForm").submit(function (event) {
        event.preventDefault();
        $.post("/handler", $(this).serialize());
      })
    })
    </script>
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2