尝试在Go中设置cookie时,出现CORS错误,表明访问控制允许凭据未设置为true

尝试在Go中设置cookie时,出现CORS错误,表明访问控制允许凭据未设置为true,go,cookies,cross-domain,Go,Cookies,Cross Domain,我想在围棋中放一块饼干。当我运行代码时,浏览器中会出现CORS错误,说明“访问控制允许凭据”设置为“无”,需要为true。然而,在我的代码中,它被设置为true。你知道问题可能是什么吗 CORS误差 在获取时访问'http://127.0.0.1:8000/signup“起源”http://127.0.0.1:8080“”已被CORS策略阻止:对飞行前请求的响应未通过访问控制检查:响应中“访问控制允许凭据”标头的值为“”,当请求的凭据模式为“”时,该值必须为“真”“包括”。 Go代码片段 客户端

我想在围棋中放一块饼干。当我运行代码时,浏览器中会出现CORS错误,说明“访问控制允许凭据”设置为“无”,需要为true。然而,在我的代码中,它被设置为true。你知道问题可能是什么吗

CORS误差
在获取时访问'http://127.0.0.1:8000/signup“起源”http://127.0.0.1:8080“”已被CORS策略阻止:对飞行前请求的响应未通过访问控制检查:响应中“访问控制允许凭据”标头的值为“”,当请求的凭据模式为“”时,该值必须为“真”“包括”。

Go代码片段 客户端JS
Sideshowbarker完全正确,谢谢。我没有将handlers.AllowCredentials()传递到handlers.CORS,这导致了上述错误

固定代码:

creds:=handlers.AllowCredentials()
log.Fatal(http.ListenAndServe(:8000),handlers.CORS(头,方法,源,creds)(r)))

localhost
127.0.0.1
不是同一个主机,因此它将其视为一个跨源请求。@Adrian抱歉,现在这两个主机都是127.0.0.1。我们已更新了帖子。还是有错误。谢谢在具有
处理程序.AllowedOrigins(…)
调用的同一位置,是否尝试添加
处理程序.allowredentials(true)
func signup(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    w.Header().Set("Access-Control-Allow-Credentials", "true")

    if r.Method == http.MethodOptions {
        return
    }
}
r := mux.NewRouter()

    header := handlers.AllowedHeaders([]string{"X-Requested-With", "Content-Type", "Authorization", "Access-Control-Allow-Credentials", "Access-Control-Allow-Origin"})
    methods := handlers.AllowedMethods([]string{"GET", "POST", "PUT", "DELETE", "OPTIONS"})
    origin := handlers.AllowedOrigins([]string{"http://127.0.0.1:8080"})

r.HandleFunc("/signup", signup).Methods("POST", "OPTIONS")

log.Fatal(http.ListenAndServe(":8000", handlers.CORS(header, methods, origin)(r)))
fetch(`${URL_API}/signup`, {
        method: 'post',
        credentials: 'include',
        headers: {
            "Content-type": "application/json"
        },
        body: JSON.stringify(formData)
    })
    .then(response => response.json())
    .then((data) => console.log(data))
    .catch(function (error) {
        console.log('Request failed', error);
    });