嗨,谢谢你,
我在main.js中使用以下功能发送联系我们的电子邮件。成功后,我只是重定向回家。
如何向客户端发送简单警报("电子邮件已发送:成功");到客户端浏览器。
来自main.js的
app.use(bodyParser.json());
app.get('/sendMail', function(req, res){
res.sendFile("index.html"); // html file is within public directory
});
app.post('/sendMail',function(req,res){
var username = req.body.name;
var subject = req.body.subject;
var fromEmail = req.body.email;
var toEmail = 'support@domain.my';
var bodyMessage = req.body.message;
// var htmlData = 'Hello: ' + toEmail;
var mg = new Mailgun('key-0000000000000000000');
mg.sendText(fromEmail, toEmail,
'CS: '+subject,
bodyMessage,
fromEmail, {},
function(err) {
if (err)res.send('NOT SENT: STRUGGLE EVERYDAY');
else res.redirect('/');
// res.json({success: true});
// else res.send('Success: EMAIL SENT From: '+fromEmail+' - To: '+toEmail);
});
});
从我的index.html页面按钮发送..
<!-- START CONTACT FORM -->
<form method="POST" action="sendMail" accept-charset="UTF-8" class="contact-form" id="jvalidate" novalidate="novalidate">
<div class="row">
<div class="col-sm-12 col-md-4">
<div class="field-holder">
<input class="form-control input-lg field" placeholder="John Snow" name="name" type="text">
</div>
</div>
<div class="col-sm-12 col-md-4">
<div class="field-holder">
<input class="form-control input-lg field" placeholder="yourEmail@Address.com" name="email" type="email">
</div>
</div>
<div class="col-sm-12 col-md-4">
<div class="field-holder">
<input class="form-control input-lg field" placeholder="Subject" name="subject" type="text">
</div>
</div>
<div class="col-md-12 col-xs-12">
<div class="field-holder">
<textarea class="form-control" placeholder="Some text" name="message" cols="50" rows="10"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<div class="alert alert-success" role="alert" style="display: none;">
Message has been successfully sent.
</div>
<div class="alert alert-danger" role="alert" style="display: none;">
<div class="alert-text">Server error</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<button class="btn btn-primary btn-contact" type="submit" value="SEND MESSAGE">SEND MESSAGE</button>
</div>
</div>
</form>
<!-- END CONTACT FORM -->
如果代码不允许,我可以为此安装任何模块吗?
节点和js的新功能。谢谢你的帮助。
答案 0 :(得分:1)
res.json({success: true});
在前端,您可以使用promise(JavaScript,jQuery,AngularJs..Whatever)
获得响应,并检查success
属性以获取进一步的总线逻辑。