我在一個請求上設置會話并在另一個頁面請求上獲取它們時遇到問題。我正在使用 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 回答
- 0 關注
- 180 瀏覽
添加回答
舉報
0/150
提交
取消