前端cookie使用注意事项

 

1、cookie可以做本地缓存,但是有大小限制(4k);

2、HTTP请求发送时,会把保存在该请求域名下的所有cookie值一起发送给web服务器;

--导致错误:The size of the request headers is too long

请求时,会把域名下所有cookie信息提交到服务器,这是默认设置,所以要么页面卡死,要么The size of the request headers is too long

所以替代方法:

function localstorage(key, value) {
    if (!window.localStorage) {
        return;
    }
    var storage = window.localStorage;
    if (typeof value == "undefined") {
        return storage[key];
    } else {
        storage[key] = value;
    }
}

 var _fishPriceCookie = localstorage("yhq_todayDatePrice"); //$.cookie("yhq_todayDatePrice");

localstorage("yhq_todayDatePrice", JSON.stringify({ date: date, data: _data }));

posted @ 2016-12-08 09:57  wjl910  阅读(98)  评论(0)    收藏  举报