简体   繁体   English

获取反应应用程序中的调用不返回正文,相同的调用适用于 postman

[英]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两个问题

  1. You don't return anything in the first then() and您不会在第一个then()
  2. You need to use 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.

相关问题 为什么在我的反应应用程序中使用 fetch 发出的调用在邮递员中成功执行时会失败? - Why post call made using fetch in my react app fails when the same is successfully executed in postman? 通过对 axios 调用做出反应来启动 php 会话并不能使会话保持活动状态,但是与邮递员一起执行它可以正常工作 - Starting a php session from react with an axios call does not keep the session alive, but doing it with postman works just fine Postman 适用于 App Script,React forms 失败 - Postman works fine with App Script, React forms fail API 在 postman 中工作正常,但在反应的前端部分中却没有 - API works fine in postman but not in frontend part of react Laravel Passport 在 PostMan 上工作正常,但在反应原生应用程序中返回 401? - Laravel Passport works fine on PostMan, but returns 401 inside the react native app? 在Codepen与Postman中获取API调用 - Fetch API call in Codepen vs. Postman Req.body 使用我自己的 fetch 为空,但适用于 postman - Req.body is empty using my own fetch, but works with postman React Class如何在promise获取调用后返回布尔值 - React Class how to return a Boolean after a promise fetch call 内置的 React 应用程序工作正常,但开发模式下的相同应用程序(npm start)工作不正常 - Built React App works fine but same App in Dev mode (npm start) works not properly Postman 中的 API 工作正常,但在本机反应中出现 422 错误 - API in Postman works fine but getting 422 error in react native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM