weixin_33708432 2014-01-24 04:52 采纳率: 0%
浏览 27

在Node.js中读取Ajax值

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.

  • 写回答

2条回答 默认 最新

  • weixin_33719619 2014-01-24 06:39
    关注

    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?

    评论

报告相同问题?