我正在嘗試使用以下標頭設定POST https://target-endpoint.com/authentication從另一個來源(https://origin.com)呼叫端點(),但仍然出現 CORS 錯誤。
我的設定有什么錯誤嗎?
authentications_controller.rb
def create
response.headers['access-control-allow-origin'] = 'https://origin.com'
// some impl
end
routes.rb
Rails.application.routes.draw do
post :authentication, to: "authentications#create", via: :options
end
鉻控制臺中的錯誤
Access to fetch at 'https://target-endpoint.com/authentication' from origin 'https://origin.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
chrome 網路選項卡中的錯誤

如果您有任何想法,請告訴我。謝謝,
其他條件。
- heroku,堆疊 18
- 紅寶石 2.6.4
- 導軌 6.0.4.8
uj5u.com熱心網友回復:
在瀏覽器發出跨域 POST 請求之前,它們首先執行所謂的 CORS 預檢請求,以確保 POST 的目標允許該請求。
為此,瀏覽器OPTIONS向 URL 發出請求并檢查回應的 CORS 標頭。只有當這個預檢請求的回應頭表明該請求被允許時,瀏覽器才會執行實際的POST請求。
對您而言,這意味著您的create操作(針對POST請求)不會收到請求,除非您也先回復OPTIONS請求。
rack-cors雖然您可以“手動”實作這一點,但使用現有的 CORS 實作(例如gem)通常是一個更好的主意。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/472675.html
標籤:轨道上的红宝石 红宝石 谷歌浏览器 heroku 科尔斯
上一篇:在Rails5中優化資料庫查詢
下一篇:從陣列中查找子集
