cookie JSON解析时输入错误意外结束

时间:2015-02-02 16:03:26

标签: javascript json cookies

我正在尝试将保存在cookie上的JSON字符串解析回JSON对象。从cookie中解析JSON字符串会在解析未保存到cookie的相同字符串时读取错误。

此代码提供“意外的输入结束”

$(document).ready(function(){
var keyStore={
"hello":"there"
}
document.cookie=JSON.stringify(keyStore);
var parsedData=JSON.parse(document.cookie);//Error is on this line!!!
console.log(parsedData);
});

虽然此代码可以正常使用

$(document).ready(function(){
var keyStore={
"hello":"there"
}
var stringifiedText=JSON.stringify(keyStore);
var parsedData=JSON.parse(stringifiedText);
console.log(parsedData);
});

0 个答案:

没有答案