javascript - Uncaught SyntaxError: Unexpected token u in JSON at position 0

javascript – Uncaught SyntaxError: Unexpected token u in JSON at position 0

javascript – Uncaught SyntaxError: Unexpected token u in JSON at position 0

Try this in the console:

JSON.parse(undefined)

Here is what you will get:

Uncaught SyntaxError: Unexpected token u in JSON at position 0
    at JSON.parse (<anonymous>)
    at <anonymous>:1:6

In other words, your app is attempting to parse undefined, which is not valid JSON.

There are two common causes for this. The first is that you may be referencing a non-existent property (or even a non-existent variable if not in strict mode).

window.foobar = {some:data};
JSON.parse(window.foobarn)  // oops, misspelled!

The second common cause is failure to receive the JSON in the first place, which could be caused by client side scripts that ignore errors and send a request when they shouldnt.

Make sure both your server-side and client-side scripts are running in strict mode and lint them using ESLint. This will give you pretty good confidence that there are no typos.

As @Seth Holladay @MinusFour commented, you are parsing an undefined variable.
Try adding an if condition before doing the parse.

if (typeof test1 !== undefined) {
test2 = JSON.parse(test1);
}

Note: This is just a check for undefined case. Any other parsing issues still need to be handled.

javascript – Uncaught SyntaxError: Unexpected token u in JSON at position 0

localStorage.clear()

Thatll clear the stored data. Then refresh and things should start to work.

Related posts on java script :

Leave a Reply

Your email address will not be published.