У меня есть массив следующего типа:
var survey = [
{question_id: "survey_id_1", question: "вава", answer: "1", date_first: null, date_second: null},
{question_id: "survey_id_2", question: "авав", answer: "2", date_first: null, date_second: null},
{question_id: "survey_id_3", question: "йцйц", answer: "3", date_first: null, date_second: null},
{question_id: "survey_id_4", question: "уцуц", answer: "4", date_first: null, date_second: null},
{question_id: "survey_id_1", question: "вава", answer: "1", date_first: null, date_second: null},
{question_id: "survey_id_2", question: "авав", answer: "2", date_first: null, date_second: null},
{question_id: "survey_id_3", question: "йцйц", answer: "3", date_first: null, date_second: null},
{question_id: "survey_id_4", question: "уцуц", answer: "4", date_first: null, date_second: null},
{question_id: "survey_id_1", question: "вава", answer: "1", date_first: null, date_second: null},
{question_id: "survey_id_2", question: "авав", answer: "2", date_first: null, date_second: null},
{question_id: "survey_id_3", question: "йцйц", answer: "3", date_first: null, date_second: null},
{question_id: "survey_id_4", question: "уцуц", answer: "4", date_first: null, date_second: null},
{question_id: "survey_id_1", question: "вава", answer: "1", date_first: null, date_second: null},
{question_id: "survey_id_2", question: "авав", answer: "2", date_first: null, date_second: null},
{question_id: "survey_id_3", question: "йцйц", answer: "3", date_first: null, date_second: null},
{question_id: "survey_id_4", question: "уцуц", answer: "4", date_first: null, date_second: null},
{question_id: "survey_id_1", question: "вава", answer: "1", date_first: null, date_second: null},
{question_id: "survey_id_2", question: "авав", answer: "2", date_first: null, date_second: null},
{question_id: "survey_id_3", question: "йцйц", answer: "3", date_first: null, date_second: null},
{question_id: "survey_id_4", question: "уцуц", answer: "4", date_first: null, date_second: null},
// Если добавлять элементы выше, то они отлично заносятся в кукис. Но любые элементы далее просто
// не заносятся, если их больше 20. Уменьшая количество до 20 все ок. Никаких ручных ограничений я не ставил!
{question_id: "survey_id_1", question: "вава", answer: "1", date_first: null, date_second: null},
{question_id: "survey_id_2", question: "авав", answer: "2", date_first: null, date_second: null},
{question_id: "survey_id_3", question: "йцйц", answer: "3", date_first: null, date_second: null},
{question_id: "survey_id_4", question: "уцуц", answer: "4", date_first: null, date_second: null},
]
Я перевожу его в строку JSON и хочу добавить её в кукис.
// Эта функция заменяет {}, которые не позволительны в JSON на []
// и располагает ключ значение ключ значение по их порядку
function survey_to_arrays(obj) {
// transforms
// [ {"x" : "y", "c" : "z"} ]
// to
// [ ["x", "y", "c", "z"] ]
var d, val
var result = []
for (var i = 0; i < obj.length; i++) {
d = obj[i]
result.push([])
for (var key in d) {
val = d[key]
result[result.length-1].push(encodeURIComponent((key))
result[result.length-1].push(encodeURIComponent((val))
}
}
return encodeURIComponent(JSON.stringify(result))
}
// заношу в кууис
document.cookie = "answers=" + survey_to_arrays(survey)
Но больше 20 элементов он не хочет заносить в кукис \ переводить. Я знаю, что у кукис нет ограничений по длине, и, наверное, ошибка в другом. (Если убирать мою функциию и просто
document.cookie = "answers=" + survey_to_arrays(survey)
То ничего не заносится.
Что не так и как мне занести этот массив в кукис?
Правка 1
Функция нормально возвращает строку, которая правильно кодирована для передачи в кукис. Значит дело все-таки в присваивании на строке document.cookie = ...