Express.js - 在同一页面上发送警报作为回复

vla*_*zam 6 javascript node.js express

我在视图上有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?
}
Run Code Online (Sandbox Code Playgroud)

我该如何发送提醒?我应该发送什么样的回复?

谢谢!

Yal*_*ber 7

您需要做的是向您的快速服务器发送ajax请求并评估响应并相应地提醒用户.您将使用其他编程语言执行此客户端部分.

例如.在jquery客户端部分你可以做到这一点

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

在你的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});
});
Run Code Online (Sandbox Code Playgroud)