【问题标题】:Express.js - Send an alert as a response while staying on same pageExpress.js - 在同一页面上发送警报作为响应
【发布时间】:2013-08-26 16:40:33
【问题描述】:

我在一个视图上有 3 个表单,提交时,会在 mysql 数据库中添加新条目。我想在添加条目时发送一条提示“您已成功添加 X”,而无需从页面导航。

// Form to be Submitted
<form method="post" action="route/action/">
    <input type="text" name="name">
</form>


// Route
exports.action = function (req, res) {

    client.query();

    // What kind of response to send?
}

如何发送警报?我应该发送什么样的回复?

谢谢!

【问题讨论】:

  • 您只需通过 ajax 发送表单,发送 JSON 响应并根据数据显示警报。或者,由于您使用的是节点,您可能想看看socket.io ...
  • 通过 ajax (jQuery) 发送变量并返回 res.json(true) 并在 SUCCESS 函数中弹出警报?

标签: javascript node.js express


【解决方案1】:

您需要做的是向您的快速服务器发出 ajax 请求并评估响应并相应地提醒用户。这个客户端部分你可以像其他编程语言一样做。

例如。在 jquery 客户端部分你可以这样做

$.ajax({
 url: 'route/action/',
 type: "POST",
 data: 'your form data',
 success: function(response){
  alert('evaluate response and show alert');
 }
}); 

在您的 epxress 应用中,您可以拥有类似的东西

app.post('route/action', function(req, res){
  //process request here and do your db queries
  //then send response. may be json response
  res.json({success: true});
});

【讨论】:

    猜你喜欢
    • 2011-09-07
    • 2016-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多