无法读取localStorage [英] Fail to read localStorage

查看:98
本文介绍了无法读取localStorage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 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 。有人知道发生了什么吗?根据 US / docs / Web / API / Window / localStoragerel =nofollow noreferrer> https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage localStorage特定于一份文件的来源。你能否确认你正在从localStorage中读取一个与你写的值相同的来源的值?


I have a plunker to set a value in localStorage:

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

And I have a code http://www.addbba.com/testLocalStorage.html to get it:

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

Oddly, the console shows the value is null. Does anyone know what happened?

解决方案

According to https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage localStorage is specific to a document's origin. Can you confirm that you are reading a value from localStorage using the same origin as where you wrote the value?

这篇关于无法读取localStorage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆