req.body总是一个空对象:{}

问题描述 投票:0回答:1

我无法弄清楚为什么我的快递应用程序永远不会从客户端获取正确的body数据。对于快递,我有以下内容:

app.use((req, res, next) => {
    res.header("Access-Control-Allow-Origin", "*");
    next();
});
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.post('/authenticate', async (req, res) => {
    debugger;
    res.send(req.body);
});

对于我的前端,我只运行以下代码:

await fetch("http://localhost:3000/authenticate", {
    method: "POST",
    body: "anything"
})

但在请求处理程序中,req.body始终是{}。我尝试发送字符串,JSON对象,字符串化JSON对象,但无论如何,它总是出现在{}

我唯一能想到的是前端是在一个不同的领域(它在http://localhost:4200上)。但我非常怀疑这是个问题。

http express post fetch
1个回答
0
投票

看起来你实际上并不是在发送JSON。这看起来像是在发送原始字符串anything,它不是JSON。

© www.soinside.com 2019 - 2024. All rights reserved.