简体   繁体   English

我无法从Java脚本中的会话存储中加载数组

[英]i am not able to load array from session storage in java script

I am not able to restore a created array in html and reuse it again. 我无法在html中恢复创建的数组并再次使用它。 I have used Json but do not work with me. 我曾经用过杰森,但不与我合作。 in the below code. 在下面的代码中。 I'm trying to reload items which is an array created in another page. 我正在尝试重新加载在另一个页面中创建的数组的项目。 when i try to load it, it did not work. 当我尝试加载它时,它不起作用。 how can i fix this? 我怎样才能解决这个问题? does json need header file? json需要头文件吗? Thank you. 谢谢。

$( document ).ready(function() {
    var count=sessionStorage.getItem('items');      
)};

You have to convert your array to a string before setting and back again when your get the item 您必须在设置之前将数组转换为字符串,然后在获得项目时再次返回

var letters = ['a', 'b', 'c'];
sessionStorage.setItem("letters", JSON.stringify(letters));
var storedLetters = JSON.parse(sessionStorage.getItem("letters"));

console.log(storedLetters);

The sessionStorage property allows you to access a session Storage object for the current origin.It is two way process first of all stringify object before storing to session and second parse to getting back . sessionStorage属性允许您访问当前来源的会话存储对象。这是一种双向处理,首先是将对象stringify然后再存储到会话,然后parse返回。

     var user = {'name':'John'};
     sessionStorage['user'] = JSON.stringify(user);
     console.log(sessionStorage['user']);
     var obj = JSON.parse(sessionStorage['user']);
     console.log(Object.keys(obj).length);

Here is fiddle . 这是小提琴 Opening a page in a new tab or window will cause a new session to be initiated . 在新选项卡或窗口中打开页面将导致启动新会话 More About session storage. 有关会话存储的更多信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我无法通过AJAX将会话存储数据发送到服务器 - I am not able to send session storage data through AJAX to the server 我可以将数据从Spring引导控制器传递到html页面,但是如何使用Java Script访问数据? - I am able to pass the data from Spring boot controller to html page, but how do I access the data in Java Script? PHP:如何从文件夹中读取文件名并将此文件名加载到Java脚本数组中? - PHP: How can I read from a folder filenames and load this file names in a Java script array? 我能够将文本数据加载到.js文件中的javascript数组中,但不能加载到html中 - I am able to load text data into a javascript array in .js file but not inside html 我可以在Java SDK中使用JavaScript吗? - Am I able to use JavaScript with the Java SDK? 为什么我可以使用Cordova Contact API从Android手机加载通讯录,而不能在ios上加载通讯录? - Why am I able to load Contacts from my Android phone with the Cordova Contact API but not on ios? 我正在从 API 请求数据。 目前,我以对象数组的形式获取数据,但无法在浏览器上呈现输出 - I am requesting data from an API. Currently, I am getting data as an array of objects but I'm not able to render output on the browser 我无法使用 map api 数据数组 我在过去两天尝试了几种方法 - i am not able to map the api data array i have tried several methods from last two days 如何从此脚本中的JSON数组加载图像? - How can I load an image from a JSON array in this script? 从会话存储重新加载页面上的预填充表单数据 - pre populate form data on page re load from the session storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM