CORS..Angular和API平台Symfony出现问题

问题描述

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

我在nelmio cors上使用API​​平台,这是我的配置

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;
  }
)

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

enter image description here

解决方法

您的缩进不正确,默认值和路径在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