【问题标题】:Angular 9 - PUT requests are not sending the bodyAngular 9 - PUT 请求不发送正文
【发布时间】:2020-07-14 19:00:03
【问题描述】:
// any component
async submit(value: any) {
  await this.guildService.saveGuild(this.guild.id, value);
}
...
// guild.service.ts
saveGuild(id: string, value: any) {
  return this.http.put(`${this.endpoint}/${id}?key=${this.key}`, value).toPromise();
}

API 路由

router.put('/:id', async (req, res) => {
    try {        
        const id = req.params.id;
        validateGuildManager(req.query.key, id);

        const updatedGuild = await SavedGuild.findByIdAndUpdate(id, req.body).lean();

        res.json(updatedGuild);
    } catch { res.status(400).send('Bad Request'); }
});

我尝试了许多不同的 value 组合,但 API 上未定义正文。

回购:https://github.com/theADAMJR/2pg-dashboard - 不包括提交,但问题仍然存在

【问题讨论】:

    标签: node.js angular typescript http


    【解决方案1】:

    听起来您没有正文解析器中间件。如果不使用正文解析器,正文将始终为空/未定义。

    对我来说,角度方面看起来不错,您可以通过查看浏览器网络请求选项卡中的请求并观察请求正文是否存在来验证

    快递试试https://expressjs.com/en/resources/middleware/body-parser.html

    对于 koa 试试 https://www.npmjs.com/package/koa-bodyparser

    【讨论】:

    • 我忘记了正文解析器。谢谢!
    猜你喜欢
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-18
    • 2021-03-12
    • 2019-05-25
    • 2014-10-03
    • 2014-01-15
    • 1970-01-01
    相关资源
    最近更新 更多