0

I have an HTML application where Im using AJAX to display the output in the same page after clicking the submit button. Before submitting Im able to get the values passed in the HTML form using

 app.use(express.bodyParser());

    var reqBody = req.body;

How can I read the output from the same HTML page after the submit button click.

user87267867
  • 1,409
  • 3
  • 18
  • 25

2 Answers2

0

Consider you have the following form:

<form id="register"action="/register" method="post">
    <input type="text" name="first_name"/>
    <input type="text" name="last_name"/>
    <input type="text" name="email"/>
    <input type="submit" value="register"/>
</form>

Your client side JavaScript could look like this:

var form = document.getElementById("register");
form.addEventListener("submit", function() {
    // ajax call

    // prevent the submit
    return false;
});

On server side you would be able to access the form data:

app.post("/register", function(req, res) {
    var user = {
        first_name : req.body.first_name,
        last_name  : req.body.last_name,
        email      : req.body.email
    };
});

For further reading:
How to prevent form from being submitted?

Community
  • 1
  • 1
Matthias Holdorf
  • 1,040
  • 1
  • 14
  • 19
0

You can send the response as json data from node.js to HTML page and use it in AJAX success callback.

node.js:

res.send(resObj);

HTML:

$("#form").submit(function () {
  $.ajax({
    type: "POST",
    url: 'url',
    data: formData,
    success: function (data) {
      data //contains response data
    }
  });
});
umair
  • 957
  • 1
  • 9
  • 21