简体   繁体   English

如何在不使用 NodeJS 加载页面的情况下发送 POST 请求并获取响应

[英]how to send POST request and get back response without the page loads with NodeJS

I want to send a post request to my NodeJS server to update a document in my database and get back the response, but I don't want the page loads, I just want to alert it that the process has been successfully done..... here is my code HTML:我想向我的 NodeJS 服务器发送一个发布请求以更新我数据库中的文档并取回响应,但我不想加载页面,我只想提醒它该过程已成功完成...... .. 这是我的代码 HTML:

 <form action="/products/update" method="POST" id="myFrom">
                        <button type="submit" class="btn btn-success">save</button>

    <input type="number" name="productCount">

    <input type="text" name="productName">
</form>

JS:记者:

$("#myFrom").submit(function (e) {
    $.ajax({
            success: function (data) {
                console.log(data);
            }
    });
});

NodeJS:节点JS:

router.post("/products/update", (req, res) => {
    console.log(req.body);
    res.status(200).send("success");
});

the poblem is when i press save it redirects to /products/update and types success in it, but i want the success to be an alert in the editing page问题是当我按下保存时它会重定向到/products/update并在其中键入成功,但我希望成功在编辑页面中成为一个警报

User e.preventDefault() to submit form without refreshing the page用户 e.preventDefault() 提交表单而不刷新页面

 $("#myFrom").submit(function (e) {
 e.preventDefault()
 $.ajax({
        success: function (data) {
            console.log(data);
        }
  });
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM