簡體   English   中英

SYMFONY NELMIO CORS 問題:Access-Control-Allow-Origin header 包含多個值

[英]SYMFONY NELMIO CORS issue : Access-Control-Allow-Origin header contains multiple values

我用 symfony、FOSRestBundle 和 NelmioCorsBundle 創建了一個 API,並使用 Ionic APP 連接到它。 有些路線返回 CORS 錯誤:

從 localhost/:1 來源 'http://localhost:8100' 訪問 'http://dev.myapp.com/api/login_check' 處的 XMLHttpRequest 已被 CORS 策略阻止:對預檢請求的響應未通過訪問控制檢查: 'Access-Control-Allow-Origin' header 包含多個值 'http://localhost/8100, *',但只允許一個。

我的代碼有一部分:

配置/nelmio_cors.yml:

nelmio_cors:
   defaults:
       allow_credentials: false
       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

我試圖在 .htaccess 中添加它但沒有成功:

   Header always set Access-Control-Allow-Origin: "*"
   Header always set Access-Control-Allow-Methods: "GET,POST,OPTIONS,DELETE,PUT"

有人知道這個問題嗎? 感謝您

這是一個工作示例:

nelmio_cors:
    defaults:
        allow_credentials: false
        allow_origin: []
        allow_headers: []
        allow_methods: []
        expose_headers: []
        max_age: 0
        hosts: []
        origin_regex: false
        forced_allow_origin_value: ~
    paths:
        '^/api/':
            allow_origin: ['*']
            allow_headers: ['Content-Type', 'Authorization']
            allow_methods: ['GET', 'OPTIONS', 'POST', 'PUT', 'PATCH', 'DELETE']
            max_age: 3600
        '^/':
            origin_regex: true
            allow_origin: ['%env(string:CORS_ALLOW_ORIGIN)%']
            allow_headers: ['X-Custom-Auth']
            allow_methods: ['POST', 'PUT', 'GET', 'DELETE']
            max_age: 3600
            hosts: [ '^api\.' ]

還有你在 your.env 或 .env.local 中:

CORS_ALLOW_ORIGIN='^https?://(localhost|127\.0\.0\.1)(:[0-9]+)?$'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM