[英]Setting a cookie on all subdomains in Rails 4 and Ember
We have a Rails application that is the API component running on api.domain.com
and a front-end application in Ember.js running on www.domain.com
. 我们有一个Rails应用程序,它是在
api.domain.com
运行的API组件,而在api.domain.com
一个在www.domain.com
运行的前端应用程序。
When Ember.js sends a POST request to a route in the API, /events
, we want the API to set a cookie to remember a unique user identifier. 当Ember.js将POST请求发送到API中的路由
/events
,我们希望API设置cookie来记住唯一的用户标识符。
Hence this method in the Events controller: 因此,事件控制器中的此方法:
def set_tracking_cookie
cookies[:our_company_distinct] = {
value: create_identifier,
expires: 1.year.from_now,
domain: :all
}
end
As you see, the cookie is set on the entire domain, and is set to expire in a year. 如您所见,cookie设置在整个域上,并设置为在一年后过期。
The point is that the next time Ember queries the API, it will be able to read this cookie. 关键是,下次Ember查询API时,它将能够读取此cookie。 However, this is not the case.
然而,这种情况并非如此。
Each time the front-end queries the API, the API is unable to find the cookie, nor does it show in the cookies in my developer tools. 每次前端查询API时,API都找不到cookie,也不会在我的开发人员工具的cookie中显示它。
The Ember requests set the Access-Control-Allow-Credentials
header to true
, and I can confirm that the cookie is indeed sent in the response from the API with the correct values for domain, name, path, expiry, etc. Ember请求将
Access-Control-Allow-Credentials
标头设置为true
,并且我可以确认Cookie确实是在API响应中使用域,名称,路径,有效期等的正确值发送的。
Am I missing something? 我想念什么吗?
Thanks! 谢谢!
For anyone else going through a problem sending/receiving cookies in this way, here are some things I found helpful when I was debugging and ultimately fixed the problem: 对于通过这种方式发送/接收Cookie遇到问题的其他任何人,在调试并最终解决该问题时,我发现有些有用的东西:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.