簡體   English   中英

通過post請求nodejs提交表單后重定向到其他url

[英]Redirect to other url after submitting form by post request nodejs

代碼有什么問題? 為什么在通過帖子操作提交表單后不會重定向到路由/result

索引.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');
});

表格.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>

在表單 api 處理程序中使用res.redirect("/result")重定向前端。

由於在這里您沒有使用 AJAX 調用來提交數據,而是在提交時重定向表單,因此 onClick 事件沒有用。 您可以使用 AJAX 調用提交表單數據然后重定向頁面,也可以在提交后直接使用 res.redirect() 。

暫無
暫無

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

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