0

I'm currently building a geolocation/weather page with Google maps and I'm trying to store an array of marker objects in a JS cookie.

My cookies are based around this: CookieMonster.js

And I'm using this JSON lib: json2.js

Here is my code:

var markers = [];

if (/(^|;)\s*userCookie=/.test(document.cookie)) 
{
    markers = JSON.parse(cm_readCookie("userCookie"));
}

if(!markerExistsInMarkers(marker,markers))
{
    markers.push(marker);
    cookiemonster.set("userCookie",JSON.stringify(markers),5);
}

var marker = new google.maps.Marker({
    map: map,
    icon: image,
    title: place.name,
    position: place.geometry.location
});

Each marker object also has a listener event attached to it, if that matters.

I'm getting a JSON.parse error that says "unexpected character at line 1 column 1 of the JSON data." What do I need to do to stop this error from occurring?

Thanks in advance,

Chase

Community
  • 1
  • 1
Chase
  • 165
  • 7
  • you should use `localStorage` instead of cookies, check the cookie value and run the JSON stored in through jsonlint.com – Rob M. May 13 '15 at 00:34
  • Seems like a good idea but I'm getting "cyclic object value" when trying to stringify the markers array and add it to localStorage. – Chase May 13 '15 at 00:51
  • Did you check what is stored in the cookie? I'm guessing it's an error similar to that – Rob M. May 13 '15 at 01:50
  • I needed the cycle.js from the same Git I got json2.js from. I then needed to call JSON.decyle on what was being stringified and then retrocycle on what was being parsed. – Chase May 13 '15 at 02:02
  • @Rob M. Yes, but i am also doing this. I use local storage and cookies depending on browser. IE browsers have issues with localStorage. – Casey ScriptFu Pharr Sep 01 '17 at 15:55

0 Answers0