英文:
Symfony API doesn't return any response
问题
以下是您要翻译的内容:
当尝试将Symfony API与React应用程序连接时,存在一个问题,即API拒绝返回任何响应。但是,当直接访问API链接时,响应会正确返回。
但是,尝试通过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);
}
不响应的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
的结果
英文:
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.
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);
}
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
答案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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论