gpt4 book ai didi

javascript - 如何通过 Body-parser 获取 post 值 - Express Node.js

转载 作者:太空宇宙 更新时间:2023-11-04 00:58:38 26 4
gpt4 key购买 nike

我正在使用表达 4.11.1,以及正文解析器 1.11.0。当我运行以下代码时,我得到以下输出

请建议如何获取表单值

输出

{}

server.js

var express = require('express');
var bodyParser = require('body-parser');

var app = express();
var router = express.Router();

app.use(bodyParser.json(),bodyParser.urlencoded({ extended: true }));
// Routes

app.get('/', function(req, res){
res.sendFile(__dirname + '/index1.html');
});

app.post('/', function(req, res){
console.log(req.body);
res.send(req.body);
res.sendFile(__dirname + '/index1.html');
});

app.listen(3000,function(){
console.log("Working on port 3000");
});

index1.html

<!doctype html>
<html>
<body>
<form id="frmTest" name="frmTest" action="http://localhost:3000/" method="post">
<input type="text" id="mytext" value="sadfsad fsd fsad" />
<input type="submit" id="mysubmit" />
</form>
</body>
</html>

最佳答案

要在后端检索数据,您只需将名称添加到输入字段

<!doctype html>
<html>
<body>
<form id="frmTest" name="frmTest" action="http://localhost:3000/" method="post">
<input type="text" id="mytext" name="mytext" value="sadfsad fsd fsad" />
<input type="submit" id="mysubmit" />
</form>
</body>
</html>

关于javascript - 如何通过 Body-parser 获取 post 值 - Express Node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28250316/

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