javascript - 如何让我的示例 bodyparser 工作?
问题描述
我正在尝试使用邮递员发送一个 post 方法来接收一些示例参数。但是我的示例代码无法识别req.body
,所以它总是空的。
这是我的示例代码:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser())
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/', function(req, res){
console.log(req.body)
})
app.listen(2001, function () {
console.log('Listening on port 2001!');
});
如果我像这样使用邮递员:
我总是得到一个空的 console.log
解决方案
您将参数作为查询字符串发送,因此req.body
您必须使用而不是使用:req.query
bodyParser
将在您的情况下解析x-www-form-urlencoded
,因为您正在使用:
app.use(bodyParser.urlencoded({ extended: true }));
推荐阅读
- python - pandas sort_values 抛出 ValueError:分类类别必须是唯一的
- php - 如何在 Pods 中返回特色图像的自定义缩略图大小
- android - Google Play 不断将“文本”一词注入我的数据库,我无法阻止它
- c - 为什么我的输出错误?C新手
- google-geocoding-api - 谷歌地理编码 API 返回错误的状态代码结果
- ibm-cloud-infrastructure - 获取用于 SoftLayer 帐户中的卷的字节数
- django - 在无法访问服务器的情况下使 JS 中的 Django 会话无效
- pandas - 使用 pip 安装 pandas 时出错:找不到满足要求 numpy==1.9.3 的版本
- c++ - How can I use different #defines for one project used by two different solutions without creating new configurations
- javascript - Puppeteer - 无限滚动情况