gpt4 book ai didi

javascript - req.body 未填充表单数据

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

以下文件是我尝试向我的 nodejs (express) 服务器提交 POST 请求。 req.body 不会从我的表单中填充任何类型的数据。我做了很多搜索,发现针对这个特定问题的许多解决方案包括在我的路线之前移动 bodyparser 并确保在表单字段中包含一个名称。
任何帮助表示赞赏。

app.js

[variables]
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(validator({errorFormatter: -----}));
[other middleware]
require('./passport')(app, passport);
require('./routes')(app, passport);
[start server]

index.jade

block content
h1= title
p Please register below
.error-messages
ul.errors
if errors
each error, i in errors
li.alert.alert-danger #{error.msg}
form(method='post', action='/signup', enctype='multipart/form-data')
.form-group
label Name
input.form-control(type='text', name='name', placeholder='Enter name')
.form-group
label Email
input.form-control(type='email', name='email', placeholder='Enter email')
.form-group
label Username
input.form-control(type='text', name='username', placeholder='Enter a username')
.form-group
label Password
input.form-control(type='password', name='password')
.form-group
label Confirm Password
input.form-control(type='password', name='passwordConfirm')
input.btn.btn-primary.pull-right(type='submit', name='submit', value='Sign Up')

index.js(从 app.post('/signup', ...) 调用

var name = req.body.name,
email = req.body.email,
username = req.body.username,
password = req.body.password,
passwordConfirm = req.body.passwordConfirm;

name = 'meg';

console.log('name: ' + req.body.name);
console.log('email: ' + req.body.email);
console.log('name2: ' + name);
console.log(req.body);
console.log(req.headers);
console.log(req.is('json'));

控制台日志

name: undefined
email: undefined
name2: meg
{}
{ host: 'xx.xx.xx.xxx:xx',
connection: 'keep-alive',
'content-length': '623',
'cache-control': 'max-age=0',
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
origin: 'http://xx.xx.xx.xxx:xxxx',
'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36 OPR/29.0.1795.60',
'content-type': 'multipart/form-data; boundary=---- WebKitFormBoundaryYJyUQjMbfAn9Cr64',
dnt: '1',
referer: 'http://xx.xx.xx.xxx:xxxx/signup',
'accept-encoding': 'gzip, deflate, lzma',
'accept-language': 'en-US,en;q=0.8',
cookie: 'connect.sid=s%3Au9o1F_i22gHj0ZCqpHQiKWDLBGOVU1MJ.6DdtQAqbR6sriI8zF3AjQy6y1TDMD93TMGI7fRIJ5mM' }
false

最佳答案

您为表单使用了错误的 enctype,express 默认不支持 multipart(用于文件上传),您需要使用 x-www-form-urlencoded。

form(method='post', action='/signup', enctype='application/x-www-form-urlencoded')

关于javascript - req.body 未填充表单数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30651484/

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