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?

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度