I'm attempting to add a cookie during an ajax request handler via S.addCookie but I'm not able to get it to work as expected. I have pushed a
sample project branch showing what I am trying to do. The app is just an index page with a form to let you set an arbitrary cookie. The submission is printed in the application console as well as the browser console to show the round trip.
The S.addCookie works if I set it somewhere like LiftRules.earlyInStateful where I would expect the cookie to be set during the initial request. Seems like it's just not working in ajax calls which I wouldn't have expected.
<form data-lift="form.ajax">
<div data-lift="CookieForm">
<input type="text" name="name"/>
<input type="text" name="value"/>
<button>Add Cookie</button>
</div>
</form>
This is my snippet (omitting imports) [
github]:
object CookieForm {
def render = {
var name: String = ""
var value: String = ""
def process(): JsCmd = {
val cookie = HTTPCookie(name, value)
println(s"Adding cookie $cookie")
S.addCookie(cookie)
println(S.responseCookies)
JsRaw(s"console.log('Added cookie: $name = $value')")
}
"name=name" #> SHtml.text(name, name = _) &
"name=value" #> (SHtml.text(value, value = _) ++ SHtml.hidden(process))
}
}
Everything checks out except I never see the cookie set in the Chrome developer tools. Refreshing the page doesn't get it done either. I've tried with both Lift 3.1 and Lift 2.6.
Do we have a bug, or do I have a misunderstanding?
Joe