首页 > 解决方案 > 无法从 node.js express 上的 Angular HTTPClient 模块接收正文数据

问题描述

我在 Angular 中有一个前端,在 node.js 中有一个带有 express 的后端。

我在 Angular 中执行 POST 请求,如下所示:

this.userDetails = {
  address: {
    city: "City"
    country: "CH"
    number: "8"
    road: "Road"
    zip: "1020"
  },
  details: {
    2FA: false
    email: "test@user.io"
    gender: "0"
    name: "User"
    surname: "Name"

  }
}
this.http.post(
  environment.paymentHook + '/customercreate',
  this.userDetails
).subscribe(
  res => resolve(res),
  err => resolve(err)
);

像这样在后面接收它:

app.use(express.json());
app.options('/customercreate', cors(corsOptionsDelegate), function (req, res) { 
  res.end(); 
});
app.post('/customercreate', cors(corsOptionsDelegate), async (request, response) => {
  const body = request.body; // --> this appears empty {} when it should have the this.userDetails content
});

有人知道我的问题是什么吗?

标签: node.jsangularexpress

解决方案


推荐阅读