javascript - 如何在 Javascript cookie 中存储大数据

标签 javascript cookies local-storage jstorage

您好,我是 JavaScript 新手,有一个场景,我想在 javascript cookie 上存储一些数据。

问题是 javascript cookies 只能存储 4 KB 的数据。 在寻找替代方案时,我发现了 jStorage 和 local-storage,但并非所有浏览器都支持这些。 有没有同样的解决方法。

if ($.cookie('test_data') == null ){
     test_data.push({
      qid: qid, 
      answer:  answer
      });
$.cookie("test_data",JSON.stringify(test_data) , {  expires: expires,   path: '/' });
  }
  else{
    test_data = JSON.parse($.cookie("test_data"));
    test_data = $.grep(test_data, function(e){ return e.qid != qid;   });
    test_data.push({
    qid: qid, 
    answer:  answer
    });
    $.cookie("test_data", JSON.stringify(test_data), {  expires: expires, path: '/' });
  }

这是我正在使用的示例代码。

这里的问题是,如果我的答案太大,cookie 存储就会失败。我必须存储相同的答案组。

感谢任何帮助。

我看到的一些引用链接:Related Link

最佳答案

Cookie 不能用于存储大量数据,因为它们会随每个请求传输到服务器。

那么问题来了:你到底需要cookie吗?或者只是在本地存储一些数据?

如果稍后再检查 localStorage所有现代浏览器都可用。

关于javascript - 如何在 Javascript cookie 中存储大数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32774594/

相关文章:

javascript - 通过依赖注入(inject)传递参数

javascript - 捕获 div 另一个页面或 jQuery javascript

python - 如何设置/删除跨域cookie?

javascript - PHP/JavaScript cookie 的作用就像一个指针,因此当我清除它时,我会丢失该值;如何从 cookie 中获取值?

android - AsyncStorage 数据在 Android 设备上的物理位置在哪里?

javascript - 无法将具有用户定义索引的数组保存到 localStorage

javascript - datepicker和动态表不使用jQuery函数

javascript - Spring中URL参数的加密和解密

php - HTML5 网络存储与 Cookie

Javascript 显示本地存储阵列中的数据问题