微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。