2 回答

TA貢獻1824條經驗 獲得超5個贊
Access-Control-Allow-Origin: *必須從服務器發送,而不是由客戶端發送。假設您在標準net/http處理程序函數中,請嘗試以下代碼:
func handler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
if (r.Method == "OPTIONS") {
w.Header().Set("Access-Control-Allow-Headers", "Authorization") // You can add more headers here if needed
} else {
// Your code goes here
}
}

TA貢獻1813條經驗 獲得超2個贊
首先 - 您需要在處理程序中使用模式:
w.Header().Set("Access-Control-Allow-Origin", "*")
if (r.Method == "OPTIONS") {
w.Header().Set("Access-Control-Allow-Headers", "Authorization") // You can add more headers here if needed
} else {
// Your code goes here
}
但在此之前,您需要在主“選項”中指定:
router.HandleFunc("/your_route/", your_method).Methods("POST", "OPTIONS")
這是因為您的瀏覽器執行 2 個請求 - 首先檢查使用某些標頭的能力(例如授權),下一步是發布數據
- 2 回答
- 0 關注
- 130 瀏覽
添加回答
舉報