node.js - 在 Angular 7 中,http-put 请求在后端(NodeJs)发送未定义的数据
问题描述
我想使用来自组件文件的 Http.put 请求将一些数据(JSON 格式)从前端(Angular 7)发送到后端(NodeJS)。在主控制台中,我以未定义的格式获取数据。下面分别是我的前端和后端代码。
this.http.put('/api/home', {moo:"foo",goo:"loo"}).subscribe(ccc => {
console.log("Status:" + ccc);
this.b = ccc;
})
router.put("/home", function (req, res) {
var aaa = req.body;
console.log(aaa);
res.send(cc)
});
在后端,我从 console.log(aaa) 得到未定义。我想要 foo 或 loo。
解决方案
您必须先使用正文解析器,然后再使用路由器。
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: false })); // support encoded bodies
app.use('/', router);
推荐阅读
- java - 从另一个动态创建的 ComboBox 更新在 Javafx 中动态创建的 ComboBox 的内容
- python - 用 Pandas 中的共同值填充系列?
- html - Angular FormControl 开启显示 [object 对象]
- javascript - 如何将脚本包含在 angular.json 中
- php - 使用 PHP cURL 下载和 Excel 文件
- r - 在我向 ggplot 添加标签后突然找不到因素
- javascript - 使用jquery动态确定点击了特定类的哪个按钮
- python-3.x - 索引定位器错误 IndexError:单个位置索引器超出范围
- sql - SQL 选择连接表上的最新记录
- r - R中的else和条件分类语句