Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

syntaxerror json.parse unexpected character at line 1 column 1 of the json data

i Don't know what is the problem i got above syntax error in json.parse I m using like below code

Storage.prototype.setObject = function(key, value) {
   this.setItem(key, JSON.stringify(value));
}

Storage.prototype.getObject = function(key) {
   var value = this.getItem(key);
   return value && JSON.parse(value);
}

function main() {
   var data = { 
       "a":"something1",
       "b":"something2"
   };
   sessionStorage.setObject('data',data);
   var newData = sessionStorage.getObject('data');
   console.log(newData);  
}

while calling getObject('data') i got the error in " firefox " while " no error " in chrome pls help me to figure out the problem i run above sample code separately and it works fine for me but in my project where i m doing something same it cause error.

like image 651
level_0 Avatar asked May 23 '14 18:05

level_0


People also ask

How do I fix SyntaxError JSON parse unexpected character at line 1 column 1 of the JSON data?

The "SyntaxError: JSON. parse: unexpected character" error occurs when passing a value that is not a valid JSON string to the JSON. parse method, e.g. a native JavaScript object. To solve the error, make sure to only pass valid JSON strings to the JSON.


1 Answers

I don't get any errors in either Firefox or Chrome. However, you can catch this exception for debugging by adding a try/catch block to the getObject method

Storage.prototype.getObject = function(key) {
    var value = this.getItem(key);
    if (value) {
        try {
            value = JSON.parse(value);
        } catch (err) {
            console.error("Error parsing stored data: " + err);
        }
    }
}
like image 131
pgoldrbx Avatar answered Oct 26 '22 23:10

pgoldrbx