nelmio cors捆绑软件不适用于我的symfony 3.1项目

时间:2018-08-30 11:35:45

标签: symfony cors

我有一个symfony 3 API,可以在侧面nelmio cors捆绑包中使用以处理cors错误,但是由于某些原因,nelmio捆绑包无法正常工作,并且cors错误不断弹出,我的字体是使用angular 6构建的。 / p>

这是我的symfony应用程序的帖子并获取其控制器:

/**
 * Lists all user entities.
 *
 * @Route("/", name="user_index")
 * @Method("GET")
 */
public function indexAction()
{
    $em = $this->getDoctrine()->getManager();

    $users = $em->getRepository('AppBundle:User')->findAll();

    return new JsonResponse(json_decode($this->createJson($users)));
}

/**
 * Creates a new user entity.
 *
 * @Route("/new", name="user_new")
 * @Method({"POST"})
 */
public function newAction(Request $request)
{
    $user = new User();
    $content = $request->getContent();
    if (!empty($content)) {
        $em = $this->getDoctrine()->getManager();
        $this->normalize($content, User::class, $user);
        $em->persist($user);
        $em->flush();

        return  new JsonResponse(array("create"=>"create"));
    }

    return new JsonResponse(array("create"=>"failed"));
}

这是我添加到config.yml中的nelmio配置:

nelmio_cors:
  defaults:
    allow_origin: ['*']
    allow_methods: ['POST', 'PUT', 'GET', 'DELETE', 'OPTIONS']
    allow_headers: ['content-type', 'authorization']
    expose_headers: ['link']
    max_age: 3600
  paths:
    '^/': ~

这是我在chrome的检查工具的“网络”标签中获得的请求标头:

Provisional headers are shown
Accept: application/json, text/plain, */*
Content-Type: text/plain
Origin: http://localhost:4200
Referer: http://localhost:4200/add
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36

0 个答案:

没有答案