我用Jquery发送表单,但在某些情况下,表单信息存储在cookie中,数据被序列化,之后我想从cookie中发送信息读取。
如何从Cookie中再次发送该数据?
我使用这2个功能来处理cookie。使用$ .ajax发送表单时,我将数据设为'数据:readCookie(" cookiename")' :
function createCookie(name, value, days) {
var expires;
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
} else {
expires = "";
}
document.cookie = encodeURIComponent(name) + "=" + encodeURIComponent(value) + expires + "; path=/";
}
function readCookie(name) {
var nameEQ = encodeURIComponent(name) + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) === ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) === 0) return decodeURIComponent(c.substring(nameEQ.length, c.length));
}
return null;
}
答案 0 :(得分:0)
我最后确实喜欢这个,还包括一些我必须在POST中发送的额外值:
var data_voto=JSON.parse(readCookie('votos'));
for (var key in data_voto) {
var obj = data_voto[key];
var cont = 0;
for (var prop in obj) {
// important check that this is objects own property
// not from prototype prop inherited
if(obj.hasOwnProperty(prop)){
if(cont==0){
data_array.push({"name": obj.name, "value": obj.value});
cont++;
}else{
cont = 0;
}
}
}
}