我无法找到答案为什么在websocket请求中缺少cookie。
请给我一个提示来解决这个问题。提前谢谢。
我测试过如下。
客户代码
$.ajax({
type: 'POST',
url: 'http://127.0.0.1/sample/',
data: { "Authorization":"test"},
xhrFields: {
'withCredentials': true
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
},
success: function() {
console.log("success: " + status);
ws = new WebSocket('http://127.0.0.1:9999/ws/'); //<--i expect that this request has cookie..
},
error: function() {
console.log("error: " + status);
}
})
用golang编写的服务器代码
token := r.FormValue("Authorization")
http.SetCookie(w, &http.Cookie{Name: "Authorization", Value: token, Expires:time.Now().Add(356*24*time.Hour), Secure: false})
ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*")
ResponseWriter.Header().Set("Access-Control-Allow-Credentials", "true")
第一个回复标题 - 没有2个过程
Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin:*
Content-Length:0
Content-Type:text/plain; charset=utf-8
Date:Tue, 26 Jan 2016 07:57:17 GMT
Set-Cookie:Authorization=test; Expires=Mon, 16 Jan 2017 07:57:17 GMT