簡體   English   中英

Express.js - 在同一頁面上發送警報作為回復

[英]Express.js - Send an alert as a response while staying on same page

我在視圖上有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});
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM