获取Revel控制器返回cookie

huangapple go评论85阅读模式
英文:

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]

huangapple
  • 本文由 发表于 2014年11月4日 15:16:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/26729590.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定