请求的资源 Symfony 上不存在 'Access-Control-Allow-Origin' header

No 'Access-Control-Allow-Origin' header is present on the requested resource Symfony

我想通过前端请求访问我的后端,但我总是收到以下错误: CORS 策略已阻止从源“...”访问位于“...”的 XMLHttpRequest:对预检请求的响应未通过访问控制检查:'Access-Control-Allow-Origin' header 不存在于请求的资源。

我已经尝试使用 Nelmio Bundle 并搜索了不同的地方,但没有任何效果。我主要遵循了本教程: https://codereviewvideos.com/course/beginners-guide-back-end-json-api-front-end-2018/video/symfony-4-cors-json-api

我也尝试在我的函数中使用 headers,但结果是相同的错误消息:

//Controller
      $response = new Response();
            $response->headers->set('Content-Type', 'application/json');
            $response->headers->set('Access-Control-Allow-Origin', '*');

//nelmio_cors.yaml:
nelmio_cors:
    defaults:
        origin_regex: true
        allow_origin: ['%env(CORS_ALLOW_ORIGIN)%']
        allow_methods: ['GET', 'OPTIONS', 'POST', 'PUT', 'PATCH', 'DELETE']
        allow_headers: ['Content-Type', 'Authorization']
        expose_headers: ['Link']
        max_age: 3600
    paths:
        '^/': null

//.env File:

    ###> nelmio/cors-bundle ###
CORS_ALLOW_ORIGIN=^https?://.*?$
###< nelmio/cors-bundle ###

我发现 Azure 提供了一个内置功能来启用和管理 CORS。这个函数似乎覆盖了我试图在我的函数中使用的 CORS 包。因此,您只需转到 Azure 门户 -> 您的应用服务 -> 然后单击 CORS(在 API 下)