如何在会话存储中设置/获取/保存数据? [英] How to set/get/save data in session storage?

查看:410
本文介绍了如何在会话存储中设置/获取/保存数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在ajax调用响应成功后获得的数据的示例obj.DATA看起来像这样:

here is example of my data that I get after ajax call response is successful obj.DATA looks like this:

{
  "A43D": {
    "FIRSTNAME": "Mike",
    "EMAIL": "mjohns@gmail.com",
    "LASTNAME": "Johns"
  },
  "4E83": {
    "FIRSTNAME": "Steve",
    "EMAIL": "scook@gmail.com",
    "LASTNAME": "Cook"
  }
}

$.ajax({
   type: 'POST',
   url: 'AjaxFunctions.cfc?method=getCustomers',
   data: formData,
   dataType: 'json'
}).done(function(obj){
   console.log(obj.DATA);
   sessionStorage.setItem("customersData", JSON.stringify(obj.DATA));      
}).fail(function(jqXHR, textStatus, errorThrown){
   alert('Error: '+errorThrown);
}); 

然后我将sessionStorage转储到console.log(sessionStorage)中,我看到了:

Then I dump sessionStorage in console.log(sessionStorage) and I see this:

{
  "customersData": "{\"A43D\":{\"FIRSTNAME\":\"Mike\",\"EMAIL\":\"mjohnes@gmail.com\",\"LASTNAME\":\"Johnes\"},\"4E83\":{\"FIRSTNAME\":\"Steve\",\"EMAIL\":\"scook@gmail.com\",\"LASTNAME\":\"Cook\"}}"
}

所以我正尝试下一个:

sessionData = sessionStorage.hasOwnProperty(customersData[recordID]) ? JSON.parse(sessionStorage.getItem(customersData[recordID])) : null;

这是在函数中,我只传递记录ID,然后尝试在会话存储中访问该记录.如果我尝试console.log(sessionData),则只能看到null.我想知道如何访问会话存储内部的customerData对象中的特定键?另外我该如何在sessionStorage customerData对象中插入/编辑记录?

This is in the function where I just pass record id and then try to access that record in session storage. If I try to console.log(sessionData) all I see is null. I'm wondering how I can access specific key in customersData object inside of the session storage? Also how I would insert/edit record in sessionStorage customersData object?

推荐答案

每次您尝试为localStorage/sessionStorage保存/加载某些内容并且您知道它是一个json对象时,请始终根据情况对其进行字符串化/解析.

each time you try to save/load something for the localStorage/sessionStorage and you know it is a json object, always stringify/parse it depending on the case.

在这里,您的代码已修复,可以工作.

here you have your code fixed to work.

注意::我尝试创建一个代码段,但由于我们无法访问沙箱的sessionStorage,因此无法正常工作.

NOTE: I tried to create a snippet but it didn't work because we can not access to the sessionStorage of a sandbox.

注意2::始终检查要解析的数据,如果存储中不存在该记录,它将返回null.

NOTE2: always check what data are you going to parse, if the record doesnt exist on the storage, it will return null.

var data = {
  "A43D": {
    "FIRSTNAME": "Mike",
    "EMAIL": "mjohns@gmail.com",
    "LASTNAME": "Johns"
  },
  "4E83": {
    "FIRSTNAME": "Steve",
    "EMAIL": "scook@gmail.com",
    "LASTNAME": "Cook"
  }
}

//here we save the item in the sessionStorage.
sessionStorage.setItem("customersData", JSON.stringify(data));


//now we retrieve the object again, but in a string form.
var customersDataString = sessionStorage.getItem("customersData");
console.log(customersDataString);

//to get the object we have to parse it.
var customersData = JSON.parse(customersDataString);
console.log(customersData);

这篇关于如何在会话存储中设置/获取/保存数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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