javascript - 无法从html中的表单获取数据
问题描述
我想把表单中的数据发布到服务器中,最后跳转到chat.html页面。客户资料如下
<form action="chat.html" method="POST">
<input
type="text"
name="username"
id="username"
placeholder="Enter username..."
required
/>
<input
type="text"
name="room"
id="room2"
placeholder="Enter room name..."
required
disabled = "disabled"
/>
...
</form>
服务器端
app.post("chat.html", (request, response) => {
console.log(request.body);
});
我希望服务器可以接收到表单中的数据,客户端可以跳转到chat.html页面。但是,出现了无法 POST /chat.html的问题。我怎么能解决这个问题?我尝试使用
<form action="/chat" method="POST">
数据可以接收,但页面不能跳转。
解决方案
您可以将表单操作更改为路线,并在后面使用相同的路线。
在表格中;
<form action="/chat" method="POST"> ...
后端;
// render the chat.html
app.get("/chat",(request,response)=>{ response.render("chat.html")});
// post request in /chat route
app.post("/chat", (request, response) => {console.log("form posted")});
推荐阅读
- sql - 如何最好地使用 Java 和 Spring 处理动态查询参数
- python - 尝试在 google colab 中获得可重现的结果时出现“NameError: name 'session_conf' is not defined”
- python - 如何按不同的子类别计数对数据框/数据透视表进行排序?
- google-cloud-platform - 有没有办法直接访问和使用存储在 GCP 存储桶中的数据?
- excel - VBA 中的不匹配错误 - 列问题
- python - 如何修复 AttributeError:“list”属性在列表理解中没有属性“astype”?
- angular - 如何配置一个在大屏幕和小屏幕上都很好用的材质 cdk 覆盖位置策略?
- javascript - Web 扩展:有没有办法将事件对象从 BrowserAction 传递到后台脚本?
- reactjs - 使用 this.props.history.push('/') 的用户注销重定向不会重定向到所需的页面
- ruby-on-rails - Pages#index 中的 Sass::SyntaxError:错误消息未定义变量:“$enable-grid-classes”