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

node.js&表示:req.body未定义

IT培训 admin 5浏览 0评论

node.js&表示:req.body未定义

我目前正在设计一个使用express的简单浏览器应用程序。我正在尝试提取用户在下拉菜单中选择的值。我也给每个选项一个单独的值,并将形式的方法声明为/ post。但是当我尝试通过进入req.body选择哪个值时,该值是不确定的。

我认识到问题可能出在浏览类似问题(Example,Example1)的正文解析器中,但这些问题的解决方案并没有阻止req.body的定义。

这是我的应用程序构建代码

const app = express()
app.use(express.static(__dirname, ''));
app.engine('html', require('ejs').renderFile);
app.set('views', __dirname + '/public/views');
app.use(express.urlencoded());
app.set('view engine', 'html');
const server = http.createServer(app);

这是帖子处理的代码

app.get('/detailed', function(req,res){
    res.send(displayDetailed(results, req));
});
app.post('/detailed', function(req,res){
    res.send('Hello world');
    console.log(req.body);

});

[当我在localhost:8080 / detailed中发布内容时,您好世界返回的很好,但是req.body为空(返回为{})。displayDetailed函数是一个自定义函数,它返回一个html字符串,其值是从Googlesheets API的get请求中提取的。由于我不使用保存的html文档,这会影响过程吗?

回答如下:

您是否设置了供快速使用的正文解析器?您只需要npm安装body-parser,然后将它们放入您的代码即可。

const bodyParser = require('body-parser')
app.use(bodyParser.json())

希望这会有所帮助!

node.js&表示:req.body未定义

我目前正在设计一个使用express的简单浏览器应用程序。我正在尝试提取用户在下拉菜单中选择的值。我也给每个选项一个单独的值,并将形式的方法声明为/ post。但是当我尝试通过进入req.body选择哪个值时,该值是不确定的。

我认识到问题可能出在浏览类似问题(Example,Example1)的正文解析器中,但这些问题的解决方案并没有阻止req.body的定义。

这是我的应用程序构建代码

const app = express()
app.use(express.static(__dirname, ''));
app.engine('html', require('ejs').renderFile);
app.set('views', __dirname + '/public/views');
app.use(express.urlencoded());
app.set('view engine', 'html');
const server = http.createServer(app);

这是帖子处理的代码

app.get('/detailed', function(req,res){
    res.send(displayDetailed(results, req));
});
app.post('/detailed', function(req,res){
    res.send('Hello world');
    console.log(req.body);

});

[当我在localhost:8080 / detailed中发布内容时,您好世界返回的很好,但是req.body为空(返回为{})。displayDetailed函数是一个自定义函数,它返回一个html字符串,其值是从Googlesheets API的get请求中提取的。由于我不使用保存的html文档,这会影响过程吗?

回答如下:

您是否设置了供快速使用的正文解析器?您只需要npm安装body-parser,然后将它们放入您的代码即可。

const bodyParser = require('body-parser')
app.use(bodyParser.json())

希望这会有所帮助!

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论