gpt4 book ai didi

node.js - 通过 HTML 表单发出 POST 请求时请求正文为空

转载 作者:搜寻专家 更新时间:2023-10-31 22:30:50 25 4
gpt4 key购买 nike

我遇到了类似 this question 的问题和其他几个人,尽管答案对我没有帮助。我正在尝试通过简单的 HTML 表单向 Node 应用程序提交密码,但返回的请求正文始终为空。

服务器:

app.use(bodyParser.urlencoded({extended: true}));

router.post('/login', (req, res) => {
console.log(req.body);
console.log(req.headers['content-type']);
});

表格:

<form action="/login" method="post">
<input type="password" id="password">
<button type="submit">Log In</button>
</form>

如果我提交表单,我会得到以下信息:

{} // req.body
'application/x-www-form-urlencoded' // req.headers['content-type']

但是,如果我尝试 curl 端点,我会得到一个非空的 req.body:

$ curl -X POST localhost:5000/login -d 'password=testpw'

// Output
{ password: 'testpw' }
'application/x-www-form-urlencoded'

我做错了什么?

最佳答案

问题出在你的表单上

<form action="/login" method="post">
<input type="password" id="password">
<button type="submit">Log In</button>
</form>

您的input 没有name 元素

应该是

<input name="password" type="password" id="password">

关于node.js - 通过 HTML 表单发出 POST 请求时请求正文为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41531280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com