【问题标题】:Problem with CORS..Angular and API Platform SymfonyCORS..Angular 和 API 平台 Symfony 的问题
【发布时间】:2020-11-30 11:03:04
【问题描述】:

我的 CORS 有一个大问题,我测试了很多东西,但我无法解决问题。

我使用 API Platform 和 nelmio cors,这是我的配置

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', 'X-LOCALE','Content-Language']
    expose_headers: ['Link']
    max_age: 3600
paths:
    '^/': null

CORS_ALLOW_ORIGIN 我使用 、. 和任何可能的模式(例如 http://localhost:[0-9]+)进行了测试 从前面,我使用Angular 我想在backend/public/images/business-profile/file.png 下获得一个文件存储

我使用服务

getFile(){
return this.httpClient.get('http://localhost:8000/images/business-profile/5f19ca78de1b5563889896.png')
}

然后进入我用来获取它的应用组件

 this.testService.getFile().subscribe(
  (resp) => {
    this.file = resp;
  }
)

当我看到浏览器时,我有这个

【问题讨论】:

  • 请以可读的形式分享错误消息以及您的调试尝试

标签: angular symfony cors api-platform.com


【解决方案1】:

您的缩进不正确,默认值和路径在 nelmio_cors 内

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', 'X-LOCALE','Content-Language']
        expose_headers: ['Link']
        max_age: 3600
    paths:
        '^/': null

【讨论】:

  • 怎么样?请告诉我
  • nelmio_cors 是这个包的根节点,所以所有的整体都必须插入这个节点。请参考文档:github.com/nelmio/NelmioCorsBundle#configuration
  • 我认为这是一个复制/粘贴错误,因为 Symfony 可能会为这些未知键抛出错误?
  • 是的,在这种情况下 symfony 应该抛出异常
  • 我测试了每个参数所有可能的组合,我不知道为什么运行不正常。
猜你喜欢
  • 2019-08-28
  • 2018-03-01
  • 2021-09-19
  • 2018-05-19
  • 2015-05-13
  • 2018-07-10
  • 2016-10-12
  • 2021-02-04
  • 1970-01-01
相关资源
最近更新 更多