【问题标题】:Symfony2 dev server and nelmio cors bundleSymfony2 开发服务器和 nelmio cors 捆绑包
【发布时间】:2015-05-18 03:07:04
【问题描述】:

我正在使用 symfony 开发一个 API,当然我已经考虑了 cors。我安装了 nelmio-cors-bundle 但标头没有到达客户端:

我的配置是这样的:

nelmio_cors:
    defaults:
        allow_credentials: true
        allow_origin: []
        allow_headers: []
        allow_methods: []
        expose_headers: []
        max_age: 0
        hosts: []
        origin_regex: false
    paths:
        '^/api':
            allow_origin: ['*']
            allow_headers: ['*']
            allow_methods: ['POST', 'PUT', 'GET', 'DELETE']
            max_age: 3600

我得到以下标题:

Allow → GET
Cache-Control → no-cache
Connection → close
Content-Type → application/json
Date → Mon, 16 Mar 2015 03:46:41 GMT
Host → 127.0.0.1:8000
X-Debug-Token → a6ec46
X-Debug-Token-Link → /_profiler/a6ec46
X-Powered-By → PHP/5.5.21

CORS 标头不存在。

你能帮帮我吗?我在 symfony 的开发服务器上运行我的应用程序,这是一个问题吗?

谢谢。

【问题讨论】:

  • 我正在使用具有相同配置的 nelmio cors,没有问题。您是否使用 XHR 请求测试响应标头?我成功地将 nelmio cors 与基于 ionic 框架和 angular 的移动应用程序一起使用......它也在开发环境中工作。
  • 我没有测试 xhr 请求,我在 postman 中测试过。在移动设备中,电话始终允许来自 Web 视图的跨域请求。
  • 我问是因为我在这里也看不到标题,但总的来说它可以在我的移动设备上使用...
  • 我们也遇到了这个问题,似乎经过几次测试后,除非您将请求发送回原点curl 'https://www.example.com' -X OPTIONS -H 'Origin: http://localhost:9000' --verbose,否则不会发回标头,但我们仍然是一些请求工作有问题,一些不工作(不返回 cors 标头)
  • @mtt 我不明白“卷曲”部分...您解决问题了吗?我已将 nelmio 配置为允许所有方法(类似于 OriginalPoster 所做的那样),但在 chrome > network 中,发送我的 post-form 时只能看到 Allow: OPTIONS, GET, HEAD

标签: php symfony cors devserver


【解决方案1】:

正如官方文档中提到的那样:

allow_origin 和 allow_headers 可以设置为 * 以接受任何值, 然而,允许的方法必须明确列出。路径必须 至少包含一项。

这是一个样本配置:

nelmio_cors:
  defaults:
    allow_credentials: false
    allow_origin: ['^http://localhost:[0-9]+']
    allow_headers: ['*']
    allow_methods: ['POST', 'GET']
    expose_headers: ['*']
    max_age: 0
    hosts: []
    origin_regex: false
    forced_allow_origin_value: ~
  paths:
    '^/':
        origin_regex: true
        allow_origin: ['^http://localhost:[0-9]+']
        allow_headers: ['Authorization']
        allow_methods: ['POST', 'PUT', 'GET', 'DELETE']
        max_age: 3600

【讨论】:

    猜你喜欢
    • 2014-03-07
    • 1970-01-01
    • 1970-01-01
    • 2019-11-11
    • 2016-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-27
    相关资源
    最近更新 更多