javascript - 通过post请求nodejs提交表单后重定向到其他url
问题描述
代码有什么问题?/result
为什么通过 post 操作提交表单后不会重定向到路由?
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');
});
表单.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>
解决方案
res.redirect("/result")
在您的表单 api 处理程序中使用以重定向前端。
由于在这里您没有使用 AJAX 调用来提交数据,而是在提交时重定向表单,因此 onClick 事件没有用。您可以使用 AJAX 调用提交表单数据,然后重定向页面,也可以在提交后直接使用 res.redirect()。
推荐阅读
- ssis - SSIS可以在Vertica(ELT)上放置命令计算吗
- php - 如何使我的简单 Datagrid 具有默认的前端样式?
- php - AJAX php代码插入NULL而不是向数据库输入值
- c# - 无法在 VB6 的外部 DLL 调用中写入文件
- html - 按钮有故障,我必须单击两次才能显示信息,并且照片没有正确对齐,这是为什么?
- java - 带有@SqlQuery 和@RegisterBeanMapper 的Bean 映射器无法实例化JDBI V3
- c# - 如何调用 ac# 函数将值填充到 razor html 文本框中?
- java - 对象初始化 - 构造函数与类类型和方法访问的关系
- jquery - 为什么我的 jquery 幻灯片可以在除 IE 11 之外的所有浏览器中工作?
- c# - 通过脚本为粒子系统设置发射