亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

與 MysqlStore 的 Golang 會話不起作用

與 MysqlStore 的 Golang 會話不起作用

Go
瀟瀟雨雨 2021-11-01 16:38:41
我在一個請求上設置會話并在另一個頁面請求上獲取它們時遇到問題。我正在使用 mysqlstore 包,該包是在自述文件中“存儲實現”部分下的大猩猩會話 github 頁面上推薦的。https://github.com/srinathgs/mysqlstore我設置了一個簡單的請求來測試它,但它不起作用。這是代碼:家庭控制器func (this *HomeController) SetCookie(ctx types.AppContext) web.HandlerType {    return func(c web.C, w http.ResponseWriter, r *http.Request) {        w.Header().Set("Content-Type", "text/plain")        fmt.Printf("%v\n\n", r)        ctx.SetCookies(r, w)    }}func (this *HomeController) GetCookie(ctx types.AppContext) web.HandlerType {    return func(c web.C, w http.ResponseWriter, r *http.Request) {        w.Header().Set("Content-Type", "text/plain")        fmt.Printf("%v\n\n", r)        ctx.GetCookies(r)    }}類型.AppContextfunc (this *AppContext) SetCookies(r *http.Request, w http.ResponseWriter) {    fmt.Println("Setting cookies")    session, _ := this.store.Get(r, "session-name")    // Set some session values.    session.Values["foo"] = "bar"    session.Values[42] = 43    // Save it before we write to the response/return from the handler.    err := session.Save(r, w)    if err != nil {        fmt.Println("Problem Saving session data")    }}func (this *AppContext) GetCookies(r *http.Request) {    session, _ := this.store.Get(r, "session-name")    fmt.Println("Getting cookies")    // get some session values    for k, v := range session.Values {        fmt.Println("Key:", k)        fmt.Println("Value:", v)    }}main.goctx.SetStore("sessions", "/", 3600, []byte("somesecret"))....goji.Get("/set-cookie", homeCtrl.SetCookie(ctx))goji.Get("/get-cookie", homeCtrl.GetCookie(ctx))當我訪問這兩個頁面時,我沒有發現任何錯誤,并且可以在數據庫中看到一個會話已保存。但是,當我嘗試檢索保存的 cookie 時, session.Values 映射為空。我一直在看這個,無法弄清楚為什么它不起作用。這很簡單,我可以做到,根據示例,這應該可以工作,但沒有。我的代碼中是否缺少某些內容以使其正常工作?
查看完整描述

1 回答

  • 1 回答
  • 0 關注
  • 180 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號