最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

req.body.username是未定义

IT培训 admin 2浏览 0评论

req.body.username是未定义

快递JS,我用肢体解析器来获取用户名。当我尝试获取用户名,则返回undefined。谁能帮我?

app.use(session({
 cookieName: 'session',
 secret: "Shh_It's_a_secret",
 duration: 100000 * 100000
}));
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/home', (req, res) => {
if(req.body.username){
 res.send('Invalid Username, Press The Back Button To Try Again');
} else if (!req.body.username) {
  app.use(express.static(__dirname + '/NerdFestProjectHomepage.html'));
  res.sendfile('NerdFestProjectHomepage.html');
  console.log(req.body.username);
}
})

当我试图让req.body.username,则返回undefined。

回答如下:

这是一个GET请求。在快递,不可能派“身体”使用GET请求。

更改GET到POST请求:

app.post('/home', cb );

并鉴于使用:

<form action="/home" method="POST"></form>

req.body.username是未定义

快递JS,我用肢体解析器来获取用户名。当我尝试获取用户名,则返回undefined。谁能帮我?

app.use(session({
 cookieName: 'session',
 secret: "Shh_It's_a_secret",
 duration: 100000 * 100000
}));
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/home', (req, res) => {
if(req.body.username){
 res.send('Invalid Username, Press The Back Button To Try Again');
} else if (!req.body.username) {
  app.use(express.static(__dirname + '/NerdFestProjectHomepage.html'));
  res.sendfile('NerdFestProjectHomepage.html');
  console.log(req.body.username);
}
})

当我试图让req.body.username,则返回undefined。

回答如下:

这是一个GET请求。在快递,不可能派“身体”使用GET请求。

更改GET到POST请求:

app.post('/home', cb );

并鉴于使用:

<form action="/home" method="POST"></form>

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论