![](/img/trans.png)
[英]Error: No 'Access-Control-Allow-Origin' header is present on the requested resource
[英]No 'Access-Control-Allow-Origin' header is present on the requested resource Symfony
我想通過前端請求訪問我的后端,但我總是收到以下錯誤:CORS 策略已阻止從源“...”訪問“...”處的 XMLHttpRequest 政策:對預檢請求的響應未通過訪問控制檢查:請求的資源上不存在“Access-Control-Allow-Origin”header。
我已經嘗試使用 Nelmio Bundle 並搜索了不同的地方,但沒有任何效果。 我主要遵循這個教程: https://codereviewvideos.com/course/beginners-guide-back-end-json-api-front-end-2018/video/symfony-4-cors-json-api
我還嘗試在我的 function 中使用標題,但結果是相同的錯誤消息:
//Controller
$response = new Response();
$response->headers->set('Content-Type', 'application/json');
$response->headers->set('Access-Control-Allow-Origin', '*');
//nelmio_cors.yaml:
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']
expose_headers: ['Link']
max_age: 3600
paths:
'^/': null
//.env File:
###> nelmio/cors-bundle ###
CORS_ALLOW_ORIGIN=^https?://.*?$
###< nelmio/cors-bundle ###
我發現 Azure 提供了一個內置 function 來啟用和管理 CORS。 這個 function 似乎覆蓋了我試圖在我的 ZC1C4254Z574268E687A945D1 所以你只需要 go 到 azure 門戶 -> 你的應用服務 -> 並點擊 CORS (在 API 下)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.