【问题标题】:Fail to read localStorage无法读取本地存储
【发布时间】:2018-02-25 05:10:04
【问题描述】:

我有一个plunker 在 localStorage 中设置一个值:

<!DOCTYPE html>
<html>
  <script>
    localStorage.setItem('test', "hadddddha");
  </script>
</html>

我有一个代码http://www.addbba.com/testLocalStorage.html 来获取它:

<!DOCTYPE html>
<html>
  <script>
    var test = localStorage.getItem('test');
    console.log(test)
  </script>
</html>

奇怪的是,控制台显示的值为null。有谁知道发生了什么?

【问题讨论】:

  • 您的两个文件是同一个域吗?如果没有,您可能遇到了跨域问题。
  • 不,他们不是……你知道如何跨域分享价值吗?
  • 我不相信,如果你不在同一个域中,你可以使用localstorage或sessionstorage。

标签: javascript html local-storage session-storage


【解决方案1】:

根据https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage,localStorage 特定于文档的来源。您能否确认您从 localStorage 中读取的值与您写入该值的来源相同?

【讨论】:

  • 好的,plunkeraddbba 的来源不同。你知道如何跨域分享价值吗?
  • 你可以尝试发送一个跨域的 XMLHttpRequest
猜你喜欢
  • 1970-01-01
  • 2021-08-26
  • 2013-06-14
  • 1970-01-01
  • 2020-03-03
  • 1970-01-01
  • 2022-01-11
  • 2014-03-25
  • 2013-05-27
相关资源
最近更新 更多