【发布时间】:2015-06-01 10:49:57
【问题描述】:
这一行
item = JSON.parse(localStorage["item"]);
当我的本地存储没有价值时,我的控制台中显示Uncaught SyntaxError: Unexpected end of input 导致错误?我该如何解决这个问题?我知道空字符串不是有效的 json。
【问题讨论】:
-
因为空字符串是无效的json
-
@PatrickEvans 感谢您澄清这一点,但如何解决这个问题?
-
简单测试先看字符串是否为空,如果不解析
-
@PatrickEvans
localStorage["item"] !== "" ? JSON.parse(localStorage["item"]);正确吗?
标签: javascript html local-storage