[英]fetch call in react app doest not return body, same call works fine with postman
I have a React app running in localhost and backend REST APIs running in localhost.我有一个在 localhost 中运行的 React 应用程序和在 localhost 中运行的后端 REST API。 When I try to make a POST call to REST API, the call is successful.
当我尝试对 REST API 进行 POST 调用时,调用成功。 But the body in empty.
但身体里空荡荡的。
The sample code can be found below.示例代码可以在下面找到。
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));
Call is successful.通话成功。 res looks like: returned object
res 看起来像:返回 object
Same code works fine in Postman.相同的代码在 Postman 中工作正常。 Also all the GET API calls work fine from the same react app.
此外,所有 GET API 调用都可以在同一个反应应用程序中正常工作。
Two problems两个问题
then()
andthen()
和res.json()
promise to access the data.res.json()
promise 来访问数据。 try something like:尝试类似:
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.