I'm trying to send some data into a JSON object, and then into a cookie. But I'm getting this error when im trying to parse it: "SyntaxError: JSON Parse error: Unable to parse JSON string".
Here is the code causing the error:
function checkCookies() {
var message;
if(document.cookie) {
var iCookie = document.cookie.split('=');
console.log(iCookie);
var iObject = JSON.parse(iCookie[1]);
message = "Cookie finnes: " + iObject.word;
} else {
message = "Fant ikke cookie.";
}
$("#sectSavedWord").html(message);
}
And this is the code where I'm trying to create the cookie:
$("#btnSaveWords").click(function(){
var finalWord = "";
for (var i = 0; i < word.length; i++) {
finalWord += word[i];
}
document.cookie = "info=" + JSON.stringify({"word": finalWord}) + ";expires=" + getExpireDate(7);
document.location = "oppgave1_2.html";
});
Can anyone point me in the right direction here? I have tried getting this to work for days now. I have used this syntax (or at least something very similar) earlier, and it has worked earlier..