首页 > 解决方案 > 在 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。

标签: node.jsangular7http-put

解决方案


您必须先使用正文解析器,然后再使用路由器。

app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: false })); // support encoded bodies
app.use('/', router);

推荐阅读