![](/img/trans.png)
[英]Making referrer and querystring available to a frame under a different domain
[英]Making a cookie available to a sibling domain with Ember
在 Ember 應用程序中,我們使用ember-cookie
設置 cookies 。 目前, userToken
cookie 僅對www.example.com
可用。 我需要將它提供給api.example.com
並且期望下面的代碼可以工作:即使domain
(實際上是通過環境變量設置)以“www”開頭,cookie 也應該設置更高一級(並且因此也可用於api
子域)。
set cookieToken(token) {
let domain = 'www.example.com';
// Share with main domain (and API subdomain) even if host is www subdomain
if (domain.startsWith('www')) {
const apiSubdomain = domain.replace(/^www/, '');
this.cookies.write('userToken', JSON.stringify(token), { path: '/', apiSubdomain })
}
}
然而,這種方法失敗了:
我還嘗試為api.example.com
明確設置 cookie:
if (domain.startsWith('www')) {
const apiSubdomain = domain.replace(/^www/, 'api');
this.cookies.write('userToken', JSON.stringify(token), { path: '/', apiSubdomain })
}
結果是一樣的:
我該怎么做才能使兩個子域都可以訪問此 cookie?
問題是我沒有在選項 hash 中指定domain
密鑰。 以下代碼會起作用,因為domain
等效於domain: domain
:
this.cookies.write('userToken', JSON.stringify(token), { path: '/', domain })
然而,在我的例子中,我以不同的方式命名了變量,所以我需要明確指定鍵:
this.cookies.write('userToken', JSON.stringify(token), { path: '/', domain: apiSubdomain })
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.