存储没有本地存储的JSON对象?

时间:2017-08-08 19:07:16

标签: javascript json

我创建了一个购物车,但它在Microsoft Edge上不起作用。那么,我可以在没有localStorage的网站中将JSON对象存储在数组中,因为它在Microsoft边缘和IE上不起作用吗?

1 个答案:

答案 0 :(得分:2)

解决方案很简单,对对象进行字符串化并存储JSON字符串。

var cart = [{item:"socks"}];
localStorage.setItem("cart", JSON.stringify(cart));
console.log(JSON.parse(localStorage.getItem("cart")));

也适用于IE11和Edge。