![](/img/trans.png)
[英]Why post call made using fetch in my react app fails when the same is successfully executed in postman?
[英]fetch call in react app doest not return body, same call works fine with postman
我有一个在 localhost 中运行的 React 应用程序和在 localhost 中运行的后端 REST API。 当我尝试对 REST API 进行 POST 调用时,调用成功。 但身体里空荡荡的。
示例代码可以在下面找到。
const body = await fetch("url", {
method : 'POST',
headers: {
"Accept" : "application/json",
"Content-Type" : "application/json"
},
body: JSON.stringify(comp)
}).then((res) => {
console.log(res);
}).then(data => console.log(data));
通话成功。 res 看起来像:返回 object
相同的代码在 Postman 中工作正常。 此外,所有 GET API 调用都可以在同一个反应应用程序中正常工作。
两个问题
then()
和res.json()
promise 来访问数据。尝试类似:
const req = await fetch("url", {
method : 'POST',
headers: {
"Accept" : "application/json",
"Content-Type" : "application/json"
},
body: JSON.stringify(comp)
});
const data = await req.json();
console.log(data)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.