sessionStorage not storing original object

后端 未结 2 1793
情深已故
情深已故 2021-01-25 10:06

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 sam

相关标签:
2条回答
  • 2021-01-25 10:30

    Try

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

    And retrieve using:

      JSON.parse(sessionStorage.getItem('xyzObject'));
    
    0 讨论(0)
  • 2021-01-25 10:45

    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();
    
    0 讨论(0)
提交回复
热议问题