【问题标题】:submitting form data without leaving page. Express, Node.js, Pug在不离开页面的情况下提交表单数据。快递,Node.js,哈巴狗
【发布时间】:2018-03-27 04:02:18
【问题描述】:

我阅读了一篇类似的帖子,但该人正在使用 perl 和其他东西,所以它对我没有帮助。我的问题是如何提交带有节点 js/pug 的表单但仍保留在同一页面上。

在 pug 表单中,方法设置为 POST,操作设置为 /profile 在我的 nodejs 中,我正在使用

    router.post("/profile", req, res, next){
        return res.redirect("back")
     }

问题是所有这些都是重新加载页面。我想留在页面上并简单地显示一条消息“个人资料更新”。

【问题讨论】:

标签: node.js forms pug


【解决方案1】:

然后不要返回重定向。只需获取 post 数据并对其进行处理,例如,您可以返回操作的状态。但是,您应该使用 AJAX 查询而不是 stock 表单,这将始终将您重定向到操作 URL。

router.post("/profile", req, res, next){
    return updateProfile(req.body); // true or false
 }

例如,在 jQuery 中,您可以执行 AJAX 请求

$.ajax({
  type: "POST",
  url: "/profile",
  data: profile_form_data_object,
  success: function(data) {
    alert("Result of the profile update was: " + data); 
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

【讨论】:

  • 但是我如何使用从表单中收集的数据将其插入到我的 mongo 数据库中。
猜你喜欢
  • 2011-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多