admin管理员组文章数量:1287858
I have 3 forms on a view that, when submitted, add new entries in a mysql database. I would like to send an alert saying "You have successfully added X" whenever an entry is added, without navigating from the page.
// 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?
}
How can I send an alert? What kind of response should I send?
Thank you!
I have 3 forms on a view that, when submitted, add new entries in a mysql database. I would like to send an alert saying "You have successfully added X" whenever an entry is added, without navigating from the page.
// 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?
}
How can I send an alert? What kind of response should I send?
Thank you!
Share Improve this question asked Aug 26, 2013 at 7:57 vladzamvladzam 5,9186 gold badges33 silver badges36 bronze badges 2- you just could send the form via ajax, send a JSON response and display the alert based on the data. Or, since you're using node, you might want take a look at socket.io ... – gherkins Commented Aug 26, 2013 at 8:02
- 1 Send the variables via ajax (jQuery) and return a res.json(true) and in the SUCCESS function pop the alert? – vladzam Commented Aug 26, 2013 at 8:04
1 Answer
Reset to default 8What you will need to do is ajax request to your express server and evaluate the response and alert the user accordingly. This client part you would do same as other programming language.
for example. in jquery client side part you can do this
$.ajax({
url: 'route/action/',
type: "POST",
data: 'your form data',
success: function(response){
alert('evaluate response and show alert');
}
});
In your epxress app you can have something like this
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});
});
本文标签: javascriptExpressjsSend an alert as a response while staying on same pageStack Overflow
版权声明:本文标题:javascript - Express.js - Send an alert as a response while staying on same page - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741325153a2372431.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论