我有一个连接到Syfmony 3后端的反应前端。但是,当我向syfmony控制器执行获取请求时,我收到403错误。
获取请求如下所示:
componentDidMount() {
fetch('/lessons', {
method: 'get', headers: {
'Content-Type': 'application/json'
}
}).then(function(response) {
console.log("test" + response);
}).catch(function(err) {
});
控制器看起来像这样:
class LessonsController extends FOSRestController {
/**
* @Rest\Get("/lessons")
*/
public function getAction()
{
$restresult = $this->getDoctrine()->getRepository('AppBundle:Lesson')->findAll();
if ($restresult === null) {
return new View("there are no users exist",
Response::HTTP_NOT_FOUND);
}
return $restresult;
}
}
我想也许这是我的NelmioCORS捆绑的问题,这个设置如下:
nelmio_cors:
defaults:
allow_credentials: false
allow_origin: []
allow_headers: []
allow_methods: []
expose_headers: []
max_age: 0
hosts: []
origin_regex: false
forced_allow_origin_value: ~
paths:
'^/lessons/':
allow_origin: ['*']
allow_headers: []
allow_methods: ['GET']
max_age: 3600
我的security.yml看起来像这样:
security:
access_control:
- { path: ^/lessons , roles: IS_AUTHENTICATED_ANONYMOUSLY}
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
anonymous: ~
谢谢!