代码有什么问题?为什么在通过邮寄表单提交表单后没有发生路由/result
的重定向?
index.js
router.get('/form', (req, res) => {
res.sendFile(path.join(__dirname, 'form.html'));
});
router.post('/form', (req, res) => {
console.log('Submitted');
res.send('Submitted');
});
router.get('/result', (req, res) => {
res.sendFile('results Page');
});
form.html
<script type="text/javascript">
function redirect() { window.location.href='http://localhost:5000/result';}
</script>
<form action="/form" method="post">
<input type="text" name="name" placeholder="Name">
<a href="/result">
<input type="submit" name="form" onClick="redirect()")>
</a>
</form>
答案 0 :(得分:0)
在表单api处理程序中使用res.redirect("/result")
重定向前端。
由于在这里您没有使用AJAX调用来提交数据,而是在提交时重定向了表单,因此onClick事件没有用。 您可以使用AJAX调用来提交表单数据,然后重定向页面,或在提交后直接使用res.redirect()。