我在一个以react为前端,go为后端的网站上工作。web在我的域上:8080,后端api在我的域上:8081。在我的后端程序中,我将cookie设置为:
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "*")
w.Header().Set("Access-Control-Allow-Credentials", "true")
cookie := &http.Cookie{
Name: "KeepLogIn",
Value: cookieContent,
Path: "/",
Expires: time.Now().Add(10 * time.Minute),
Domain: "mydomain",
}
fmt.Println(cookie.String())
http.SetCookie(w, cookie)
在我的前端程序中,我将cookie设置为:
fetch('http://mydomain:8081/xxxx', {
method: 'POST',
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Expose-Headers': '*',
'Content-Type': 'text/plain',
'Accept': '*',
},
mode: 'cors',
cache: 'default',
body: JSON.stringify(data),
credentials: 'same-origin',
})
我在Safari, Chrome和Firefox中测试过,他们都可以接收到正确的set-cookie field,但是他们都不能设置cookie。我想这可能和交叉原点有关,但我就是想不出我能做什么。