我有一个表单,在提交时我希望成功或错误消息动态显示并向用户表明表单是否已成功提交。我看过很多与res.render()无关的SO问题,但似乎没有一个适用于我的案例。关于我的情况很困难的是,我没有收到任何错误消息,说明为什么res.render()没有按预期行事。
与-Us.ejs:
<div class="m-t-30">
<form id="widget-contact-form" action="send" role="form" method="post">
<div class="row">
<div class="form-group col-sm-6">
<label for="name">Name</label>
<input type="text" aria-required="true" name="name" class="form-control required name" placeholder="Enter your Name">
</div>
<div class="form-group col-sm-6">
<label for="email">Email</label>
<input type="email" aria-required="true" name="email" class="form-control required email" placeholder="Enter your Email">
</div>
</div>
<div class="row">
<div class="form-group col-sm-12">
<label for="subject">Subject</label>
<input type="text" name="subject" class="form-control required" placeholder="Enter your Subject">
</div>
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea type="text" name="message" rows="5" class="form-control required" placeholder="Enter your Message"></textarea>
</div>
<input type="text" class="hidden" id="widget-contact-form-antispam" name="antispam" value="" />
<button class="btn btn-primary" type="submit" id="form-submit"><i class="fa fa-paper-plane"></i> Send message</button>
</form>
<p data-animation="fadeInDown" data-animation-delay="0" id="acknowledgement"><%- success %></p>
<script type="text/javascript">
jQuery("#widget-contact-form").validate({
submitHandler: function(form) {
jQuery(form).ajaxSubmit({ });
}
});
</script>
</div>
Index.js:
const express = require('express');
const app = express();
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
app.set('view engine', 'ejs');
//Body Parse
app.use(bodyParser.urlencoded({ extended: false}));
app.use(bodyParser.json());
app.get('/contact-us', (req, res) => {
res.render('contact-us', {success: ''});
});
app.post('/send', (req, res) => {
console.log(req.body);
const ack = '<i class="fa fa-check-circle" aria-hidden="true"></i> Contact form submitted and sent succesfully.';
res.render('contact-us', {success: ack}, function() {
console.log('we just rendered');
});
});
var port = process.env.port || 3000;
app.listen(port, () => console.log("Server started on port " + port + "..."));
res.render()
函数的成功处理程序正常工作并打印到我的控制台,因此我不确定问题是什么。