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 关注
- 174 浏览
添加回答
举报
