英文:
Getting Revel controller to return a cookie
问题
我已经能够存储自定义的cookie,但是我想在会话期间保存它,并在提交表单时将其存储在数据库中。我使用的代码如下:
func (c Application) Index(id string) revel.Result {
cookie := http.Cookie{Name: "id", Value: id, Domain: "localhost", Path: "/"}
c.SetCookie(&cookie)
return c.Render()
}
但是我无法再次访问它。我该如何在不同的函数中检索它呢?
谢谢!
英文:
I've been able to store a custom cookie but I'm trying to save it for a session the store it in the database when a form is submitted. I use:
func (c Application) Index(id string) revel.Result {
cookie := http.Cookie{Name: "id", Value: id, Domain: "localhost", Path: "/"}
c.SetCookie(&cookie)
return c.Render()
}
but I cannot access it again. How could I retrieve it in a different function?
Thank you!
答案1
得分: 3
我一直都弄错了。Revel使用会话来设置cookie。
设置:
c.Session[key] = value
获取:
c.Session[key]
英文:
I was going about it all wrong. Revel sets cookies with the session.
Set:
c.Session[key] = value
Get:
c.Session[key]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论