sessionStorage不存储原始对象 [英] sessionStorage not storing original object

查看:96
本文介绍了sessionStorage不存储原始对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过执行SDK功能获得的对象。当我尝试将对象存储在会话存储中并检索对象时,检索到的对象看起来与原始对象相同,但是当我对新对象执行操作时,我收到错误。

I have an object which I'm getting by executing a function of SDK. When I try to store the object in session storage and the retrieve the object, the retrieved object looks same as original but when I perform operations on the new object I'm getting error.

var xyzObject = some_function();

sessionStorage.setItem("xyzObject",xyzObject);

var obj = JSON.parse(sessionStorage.getItem("xyzObject"));

obj.some_other_function();

显示错误 obj.some_other_function 不是一个功能。而 xyzObject.some_other_function 的效果非常好。

It is showing an error as obj.some_other_function is not a function. Whereas xyzObject.some_other_function works perfectly.

推荐答案

您无法存储sessionStorage或localStorage中的对象。唯一可能的方法是对对象进行字符串化并将其保存在sessionStorage中,并在从sessionStorage接收对象时将对象解析为JSON。

You cannot store an object in the sessionStorage or localStorage. The only possible method is to stringify the object and save that in sessionStorage and on receiving the object from sessionStorage you just parse the object to JSON.

var xyzObject = some_function();

sessionStorage.setItem("xyzObject",JSON.stringify(xyzObject));

var stringData = sessionStorage.getItem("xyzObject");

var obj = JSON.parse(stringData);

obj.some_other_function();

这篇关于sessionStorage不存储原始对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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