API 在禁用调试模式的情况下调用我的 Symfony 5 应用程序 returns CORS 错误
API call to my Symfony 5 app with debug mode disabled returns CORS error
我的 Symfony 5 应用程序有这个问题,它用作我的 API。我正在从同一域和不同子域上的另一个应用程序向我的 Symfony 应用程序发出请求。
在我的 Symfony 应用程序中将调试模式设置为 true APP_DEBUG = 1
时一切正常。但是,如果我禁用调试模式 APP_DEBUG = 0
,那么来自其他应用程序的请求会导致 CORS 错误:
Access to XMLHttpRequest at 'https://api.mamacita365.com/api/getmeals'
from origin 'https://app.mamacita365.com' has been blocked by CORS policy:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
我正在使用 nelmio/cors-bundle
- 这是 nelmio_cors.yaml
:
nelmio_cors:
defaults:
origin_regex: true
allow_credentials: true
allow_origin: ['https://api.mamacita365.com', 'https://app.mamacita365.com']
allow_methods: ['GET', 'OPTIONS', 'POST', 'PUT', 'PATCH', 'DELETE']
allow_headers: ['Content-Type', 'Authorization']
expose_headers: ['Link']
max_age: 3600
paths:
'^/': null
这是.env.local.php
:
<?php
// This file was generated by running "composer dump-env prod"
return array (
'APP_ENV' => 'prod',
'APP_DEBUG' => 0, // if equals 0 then CORS error, if equals 1 then OK
// ...
);
手动清除缓存(/var/cache/prod
文件夹)或通过命令php bin/console cache:clear --env=prod
我的 Symfony 5 应用程序有这个问题,它用作我的 API。我正在从同一域和不同子域上的另一个应用程序向我的 Symfony 应用程序发出请求。
在我的 Symfony 应用程序中将调试模式设置为 true APP_DEBUG = 1
时一切正常。但是,如果我禁用调试模式 APP_DEBUG = 0
,那么来自其他应用程序的请求会导致 CORS 错误:
Access to XMLHttpRequest at 'https://api.mamacita365.com/api/getmeals'
from origin 'https://app.mamacita365.com' has been blocked by CORS policy:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
我正在使用 nelmio/cors-bundle
- 这是 nelmio_cors.yaml
:
nelmio_cors:
defaults:
origin_regex: true
allow_credentials: true
allow_origin: ['https://api.mamacita365.com', 'https://app.mamacita365.com']
allow_methods: ['GET', 'OPTIONS', 'POST', 'PUT', 'PATCH', 'DELETE']
allow_headers: ['Content-Type', 'Authorization']
expose_headers: ['Link']
max_age: 3600
paths:
'^/': null
这是.env.local.php
:
<?php
// This file was generated by running "composer dump-env prod"
return array (
'APP_ENV' => 'prod',
'APP_DEBUG' => 0, // if equals 0 then CORS error, if equals 1 then OK
// ...
);
手动清除缓存(/var/cache/prod
文件夹)或通过命令php bin/console cache:clear --env=prod