HTML5 数据库和 localStorage 可以跨子域共享吗? [英] Can HTML5 databases and localStorage be shared across subdomains?
问题描述
我正在尝试使用 Safari 跨子域共享数据.我想使用 HTML5 数据库(特别是 localStorage 因为我的数据只是键值对).但是,似乎无法从 sub.domain.com 访问存储到 domain.com 的数据(反之亦然).这种情况有没有办法共享一个数据库?
I am attempting to share data across subdomains using Safari. I would like to use an HTML5 database (specifically localStorage as my data is nothing but key-value pairs). However, it seems as though data stored to domain.com can not be accessed from sub.domain.com (or vice versa). Is there any way to share a single database in this situation?
推荐答案
2016 年更新
这个来自 Zendesk 的图书馆对我有用.
示例:
中心
// Config s.t. subdomains can get, but only the root domain can set and del
CrossStorageHub.init([
{origin: /.example.com$/, allow: ['get']},
{origin: /://(www.)?example.com$/, allow: ['get', 'set', 'del']}
]);
注意 $
用于匹配字符串的结尾.上面示例中的正则表达式将匹配诸如 valid.example.com
之类的源,但不会匹配 invalid.example.com.malicious.com
.
Note the $
for matching the end of the string. The regular expression in the above example will match origins such as valid.example.com
, but not invalid.example.com.malicious.com
.
客户
var storage = new CrossStorageClient('https://store.example.com/hub.html');
storage.onConnect().then(function() {
return storage.set('newKey', 'foobar');
}).then(function() {
return storage.get('existingKey', 'newKey');
}).then(function(res) {
console.log(res.length); // 2
}).catch(function(err) {
// Handle error
});
检查https://stackoverflow.com/a/39788742/5064633
这篇关于HTML5 数据库和 localStorage 可以跨子域共享吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!