我知道有很多與 CORS 相關的問題,但我似乎找不到這個問題的答案。這是我的服務器端 golang 代碼(我們正在使用 github.com/rs/cors go 模塊):我們基本上有一組需要授權標頭的 api 和一些不需要的 api(將結帳與結帳視為訪客功能)allowedOrigins := []string{"http://localhost:3000", "http://localhost:3001"}//allowedHeaders := []string{"Authorization"}c := cors.New(cors.Options{AllowedOrigins: allowedOrigins, AllowCredentials: true})handler := c.Handler(r)我發現的是以下內容:// if allowcredentials is set to true, then all non auth requests go through but all auth requests return cors error// if allowedHeaders: authorization is set then all **authenticated and NON authenticated** POST requests fail. GET works fine for both cases.更具體地說:當我嘗試執行 POST 請求并設置上面的 AllowedHeaders:authorization 選項時,我得到的錯誤是未設置 AllowedOrigins(??.. 我在 PRE-FLIGHT OPTIONS 響應標頭中得到這個)。如果我評論該行(如您在上面看到的),那么非身份驗證請求將完美通過,并且 AllowedOrigins hader 將在 OPTIONS 請求中發回。
1 回答

月關寶盒
TA貢獻1772條經驗 獲得超5個贊
解決它....
https://github.com/rs/cors
有一個不錯的CorsOptions Debug:true
。我用它來檢查發生了什么,當我硬編碼允許授權進入我的服務器時,POST 請求隨后抱怨,因為我也在發送內容類型(由客戶端(axios)自動發送,我沒有' t 指定它).. 并且服務器幾乎說“我只識別授權標頭”......我添加了Content-Type
并且它現在可以工作了!
allowedHeaders := []string{"Authorization", "Content-Type"}
- 1 回答
- 0 關注
- 224 瀏覽
添加回答
舉報
0/150
提交
取消