For my project, I needed to store an array in sessionStorage and came across a helpful answer that guided me to this solution: Storing Objects in HTML5 localStorage
The process of storing the array using
sessionStorage.setItem('flavors', JSON.stringify(flavors))
works great initially. However, when navigating through the app with the back and forward buttons, I encountered an error:
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
The error seems to vary based on the elements within the array.
To avoid the error, I ended up using:
flavors = sessionStorage.getItem('flavors').split(",");
instead of JSON.parse()
. Interestingly, the string appears normal when logged to the console:
chocolate,vanilla,strawberry
I'm puzzled as to what might be causing this error. Any insights?