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

5
  • you should use localStorage instead of cookies, check the cookie value and run the JSON stored in through jsonlint.com Commented May 13, 2015 at 0: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. Commented May 13, 2015 at 0:51
  • Did you check what is stored in the cookie? I'm guessing it's an error similar to that Commented May 13, 2015 at 1: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. Commented May 13, 2015 at 2: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. Commented Sep 1, 2017 at 15:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.