简体   繁体   English

如何在Javascript Cookie中存储大数据

[英]How to Store large data in Javascript cookies

Hi I am new to Java Script and have a scenario I want to store some data on javascript cookies. 嗨,我是Java脚本的新手,有一种情况我想在javascript cookie上存储一些数据。

the probelm is javascript cookies can store only 4 KB of data. 该探针是javascript cookie,只能存储4 KB数据。 Looking for an alternate i found out jStorage and local-storage but these are not supported in all browser. 寻找替代我发现jStorage和本地存储,但并非所有浏览器都支持。 Is there a work around for same. 是否有相同的解决方法。

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: '/' });
  }

This is the sample code I am using. 这是我正在使用的示例代码。

Here problem is if my answer is to big than cookie storage fails.I have to store group of answer for same. 这里的问题是我的答案是否大于cookie存储失败。我必须存储相同的答案组。

Any help is appreciated. 任何帮助表示赞赏。

Some ref Link I saw: Related Link 我看到的一些参考链接: 相关链接

Cookies cannot be used for storing large amount of data as they get transmitted to the server with each request. Cookies不能用于存储大量数据,因为它们会随每个请求传输到服务器。

So is the question: do you need exactly cookies? 那么问题是:您是否需要Cookie? Or just to store some data locally? 还是只在本地存储一些数据?

If later then check localStorage that is available in all modern browsers. 如果以后再检查所有现代浏览器中可用的localStorage

I "think" you should create table in database to storage "data" 我“认为”您应该在数据库中创建表以存储“数据”

table "COOKIES_DATA" : [ data_id , data , data_create , date_expire ] 表“ COOKIES_DATA”:[data_id,data,data_create,date_expire]

and cookies only store data_id in table. 和Cookie仅将data_id存储在表中。

When user visit, you could get data_id in cookies to get data in database 当用户访问时,您可以在cookie中获取data_id以在数据库中获取数据

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM