【发布时间】:2021-03-23 15:21:26
【问题描述】:
我从我的 React 前端向我的 Symfony 后端发送一个请求。(以前它在纯 PHP 后端上运行良好)。
useEffect(() => {
axios.get('http://localhost:8080/real_estate_refactored_backend/main').then((response) => {
console.log(response);
})
.catch((error) => {
console.log(error.message);
});
});
URL 中主要是我的 Symfony 控制器的路由
<?php
namespace App\Controller;
use App\Entity\Estates;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class MainController extends AbstractController
{
/**
* @Route("/main", name="main")
*/
public function index(): Response
{
$entityManager = $this->getDoctrine()->getManager();
$estates = $entityManager->getRepository(Estates::class)->findAll();
$response = new Response();
$response->setContent(json_encode([
'estates' => uniqid(),
]));
$response->headers->set('Content-Type', 'application/json');
$response->headers->set('Access-Control-Allow-Origin', '*');
return new Response($response);
}
}
在 Symfony 中,与数据库的连接很好。
尽管我已经在 Symfony 中安装了 Nelmino CORS 包,并在
nelmino_cors.yaml
nelmio_cors:
defaults:
origin_regex: true
allow_origin: ['*']
allow_methods: ['GET', 'OPTIONS', 'POST', 'PUT', 'PATCH', 'DELETE']
allow_headers: ['*']
expose_headers: ['Link']
max_age: 3600
paths:
'^/': null
我仍然收到以下错误消息
我能做什么?
【问题讨论】:
-
您好,您可以尝试将路径(在 nelmio_cors.yaml 中)更改为:
'^/': ~吗? -
并擦除其他内容?是的,我做了同样的结果。
-
您是否尝试清除缓存?正如这里提到的:stackoverflow.com/a/54572925/1750964
-
是的,我试过了,但没有任何改变。
标签: php reactjs symfony cors-anywhere