Javascript 通过post请求节点提交表单后重定向到其他url
代码中有什么错误?为什么在通过post操作提交表单后,没有重定向到路由Javascript 通过post请求节点提交表单后重定向到其他url,javascript,node.js,Javascript,Node.js,代码中有什么错误?为什么在通过post操作提交表单后,没有重定向到路由/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'); });
/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>
函数重定向(){window.location.href='0http://localhost:5000/result';}
在表单api处理程序中使用res.redirect(“/result”)
重定向前端
由于在这里您没有使用AJAX调用来提交数据,而是在提交时重定向表单,因此onClick事件没有任何用处。
您可以使用AJAX调用提交表单数据,然后重定向页面,也可以在提交后直接使用res.redirect()。在表单api处理程序中使用res.redirect(“/result”)
重定向前端
由于在这里您没有使用AJAX调用来提交数据,而是在提交时重定向表单,因此onClick事件没有任何用处。
您可以使用AJAX调用提交表单数据,然后重定向页面,也可以在提交后直接使用res.redirect()。