Symfony API没有返回任何响应。

huangapple go评论67阅读模式
英文:

Symfony API doesn't return any response

问题

以下是您要翻译的内容:

当尝试将Symfony API与React应用程序连接时,存在一个问题,即API拒绝返回任何响应。但是,当直接访问API链接时,响应会正确返回。

ApiDirectURL

但是,尝试通过React应用程序获取此数据不会返回任何内容。

const api_url = "http://127.0.0.1:8000/app-api/";
const farmid = "1234567890123456789012345678901234567890";

async function getFarmInfo() {
  const response = await fetch(api_url + farmid + "/info",{
    cache: "no-store", 
    mode: "no-cors",
    method: "GET",
    headers: {
      "Accept": "application/json"
    }});
  const jsonData = await response.json();
  console.log(jsonData);
}

ServerResponse

不响应的API代码

#[Route('/info', name: 'info')]
public function info(Request $request, EntityManagerInterface $em, $fishingfarmid): Response
{

    $farm = $em->getRepository(FishingFarm::class)->findOneBy(["trueId" => $fishingfarmid]);
    $json = ["phone" => $farm->getPhone(), "regulation" => $farm->getRegulations(), "email" => $farm->getEmail(), "address" => $farm->getAddress()];
    return new JsonResponse($json);
}

然而,偶然间我发现,如果我在某个地方使用var_dump("");函数,React代码中的响应突然出现。

#[Route('/info', name: 'info')]
public function info(Request $request, EntityManagerInterface $em, $fishingfarmid): Response
{

    $farm = $em->getRepository(FishingFarm::class)->findOneBy(["trueId" => $fishingfarmid]);
    $json = ["phone" => $farm->getPhone(), "regulation" => $farm->getRegulations(), "email" => $farm->getEmail(), "address" => $farm->getAddress()];
    var_dump("");
    return new JsonResponse($json);
}

var_dump的结果

Symfony API没有返回任何响应。

英文:

When attempting to connect the Symfony API with a React application, there is an issue where the API refuses to return any response. However, when accessing the API link directly, the response is returned correctly.

ApiDirectURL

However, attempting to fetch this data through the React application does not return anything.

  const api_url = "http://127.0.0.1:8000/app-api/";
  const farmid = "1234567890123456789012345678901234567890";
  
  async function getFarmInfo() {
    const response = await fetch(api_url + farmid + "/info",{
      cache: "no-store", 
      mode: "no-cors",
      method: "GET",
      headers: {
        "Accept": "application/json"
      }});
    const jsonData = await response.json();
    console.log(jsonData);
  }

ServerResponse

Not responding API Code

   #[Route('/info', name: 'info')]
    public function info(Request $request,EntityManagerInterface $em,$fishingfarmid): Response
    {

        $farm = $em->getRepository(FishingFarm::class)->findOneBy(["trueId" => $fishingfarmid]);
        $json = ["phone" => $farm->getPhone() , "regulation" => $farm->getRegulations() , "email" => $farm->getEmail() , "address" => $farm->getAddress()];
        return new JsonResponse($json);
    }

However, by accident, I discovered that if I use the var_dump(""); function somewhere, the response in the React code suddenly appears.

#[Route('/info', name: 'info')]
public function info(Request $request,EntityManagerInterface $em,$fishingfarmid): Response
{

    $farm = $em->getRepository(FishingFarm::class)->findOneBy(["trueId" => $fishingfarmid]);
    $json = ["phone" => $farm->getPhone() , "regulation" => $farm->getRegulations() , "email" => $farm->getEmail() , "address" => $farm->getAddress()];
    var_dump("");
    return new JsonResponse($json);
}

The result of the var_dump

Symfony API没有返回任何响应。

答案1

得分: 0

I'm not sure why, but it works after removing mode: "no-cors".

async function getFarmInfo() {
const response = await fetch(api_url + farmid + "/info", {
method: "GET",
});
const jsonData = await response.json();

setFarmInfo(jsonData);

}

英文:

I'm not sure why, but it works after removing mode: "no-cors".

 async function getFarmInfo() {
    const response = await fetch(api_url + farmid + "/info",{
      method: "GET",
     });
    const jsonData = await response.json();
    
    setFarmInfo( jsonData);
  }

huangapple
  • 本文由 发表于 2023年5月17日 21:36:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76272731.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定