我在一个 View 上有 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 请求并评估响应并相应地提醒用户。您可以像使用其他编程语言一样执行此客户端部分。
例如。在 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});
});
关于javascript - Express.js - 在同一页面上发送警报作为响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18439306/