【发布时间】:2021-01-22 05:08:00
【问题描述】:
我在 /api 上下文中有 Symfony Rest API,一切正常。此外,我在 /public/uploads 目录中托管静态 pdf 文件。通过前端浏览器获取文件路径时抛出错误
Access to fetch at 'http://127.0.0.1:8000/uploads/b8b3a04a69f59a6c20c9c153281657d5.pdf' from origin 'http://192.168.8.111:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
就像 nelmio cors 没有将 Access-Control-Allow-Origin 标头添加到下载的文件中,但整个 api 运行良好,没有问题。我没有覆盖标题的 .htaccess 文件。
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', 'origin', 'accept', 'bearer']
expose_headers: ['Link']
max_age: 3600
paths:
'^/uploads':
allow_origin: [ '%env(CORS_ALLOW_ORIGIN)%' ]
allow_methods: [ 'GET', 'OPTIONS' ]
allow_headers: [ 'Content-Type', 'Authorization', 'origin', 'accept', 'bearer' ]
max_age: 3600
'^/': ~
allow_origin 在 .env 中设置为 '*'
当我运行 CORS Chrome 扩展程序时,一切正常。
您知道为什么没有将标题添加到文件中吗?
【问题讨论】:
标签: symfony cors nelmiocorsbundle