首页 > 解决方案 > 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()

标签: angularfirefoxlocal-storage

解决方案


localStorage 与 globalStorage[location.hostname] 相同,但范围仅限于 HTML5 源(方案 + 主机名 + 非标准端口)

旧的 Firefox 行为是错误的,并且该错误已得到修复。根据规范,设置 document.domain 应该对 localStorage 的行为绝对没有影响,因此在您的情况下,您尝试为不同的域设置 localStorage,这是不允许的。

有关详细信息,请参阅https://bugzilla.mozilla.org/show_bug.cgi?id=495337和 localStorage 规范。

您可以在文档中阅读更多内容


推荐阅读