angular - LocalStorage 不适用于 Firefox。获取安全错误
问题描述
我正在尝试将一些信息存储在浏览器本地存储中。我的代码在Chrome上运行良好但是,我在 Firefox 和 Safari 上遇到安全错误和以下警告
Firefox 版本:63.0.1(64 位)
堆栈跟踪 :
ERROR DOMException: "The operation is insecure." EventDetailsComponent.html:1
View_EventDetailsComponent_0
EventDetailsComponent.html:1 proxyClass compiler.js:17129
./node_modules/@angular/core/fesm5/core.js/DebugContext_.prototype.logError
core.js:20684
./node_modules/@angular/core/fesm5/core.js/ErrorHandler.prototype.handleError
core.js:12632
./node_modules/@angular/core/fesm5/core.js/ApplicationRef.prototype.tick/<
core.js:14878:54
./node_modules/zone.js/dist/zone.js/</ZoneDelegate.prototype.invoke
zone.js:388
./node_modules/zone.js/dist/zone.js/</Zone.prototype.run
zone.js:138
错误消息:
[WDS] Disconnected! vendor.js:157169:5 close
ERROR DOMException: "The operation is insecure." EventDetailsComponent.html:1
View_EventDetailsComponent_0 EventDetailsComponent.html:1 proxyClass
Request to access cookie or storage on “http://localhost:4200/” was blocked because we are blocking all storage access requests.
本地存储服务:
public getFav(): FavoriteStorage[] {
return JSON.parse(window.localStorage.getItem('favlist'));
}
private setLocalStorageFavList(favL: FavoriteStorage[]): void {
window.localStorage.setItem('favlist', JSON.stringify({ favList: favL }));
}
我试过 localStorage.setItem()/getItem() 而不是 window.localStorage.setItem() /getItem()
解决方案
localStorage 与 globalStorage[location.hostname] 相同,但范围仅限于 HTML5 源(方案 + 主机名 + 非标准端口)
旧的 Firefox 行为是错误的,并且该错误已得到修复。根据规范,设置 document.domain 应该对 localStorage 的行为绝对没有影响,因此在您的情况下,您尝试为不同的域设置 localStorage,这是不允许的。
有关详细信息,请参阅https://bugzilla.mozilla.org/show_bug.cgi?id=495337和 localStorage 规范。
您可以在文档中阅读更多内容
推荐阅读
- angular - Angular:无法映射日期字段以对其进行格式化
- java - ExecutorService 线程安全
- android - LiveData 仅在方向更改时触发
- javascript - Recharts 不会自动计算 YAxis 刻度
- apache - 如何使用 SPN、OpenShift 上的 apache2 和 kerberos SSO auth 处理不同的服务器名称
- javascript - 添加多个事件监听器ajax
- css - 带有 svg 圆圈的 Material-UI 单选按钮
- java - 尺寸屏幕问题
- c# - Xamarin iOS Binding 在运行时转换方法
- multithreading - 如何将字符串发送到 _beginthreadex 以供线程读取?