Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught SyntaxError: Unexpected token u in JSON at position 0

Only at the checkout and on individual product pages I am getting the following error in the console log:

VM35594:1 Uncaught SyntaxError: Unexpected token u in JSON at position 0     at JSON.parse (<anonymous>)     at run (layout.min.js:9)     at app.min.js:1     at main.min.js:2     at Object.execCb (require.min.js:112)     at Module.check (require.min.js:56)     at Module.<anonymous> (require.min.js:72)     at require.min.js:11     at require.min.js:74     at each (require.min.js:3) 

I am using a one page checkout extension, but when I disable that the error still shows. I thought it might had something to do with the reviews on the product page (as I moved the reviews out of the tabs), but undoing that change didn't fix the error on the product pages.

like image 500
Howli Avatar asked Oct 06 '17 20:10

Howli


People also ask

What does unexpected token in JSON at position 0 mean in Quickbooks?

“Unexpected token < in JSON at position 0” is one of the fault which users have perceived in the bulk at the time of operations as similar to as a software developer. Quite often which happens in a situation while fetch function is used for sending an API request from a client.

What is unexpected token U in JSON at position 0?

The "Unexpected token u in JSON at position 0" is a typically Javascript error that will show up in your console log if the client has been directed to execute JSON. parse() on a string beginning with u instead of a stringified object. "u" is typically a stringified version of the undefined primitive.

What does position mean in JSON?

The position in the error message indicates which byte within the file the error occurs around.

What is uncaught SyntaxError unexpected token?

The "Uncaught SyntaxError: Unexpected token" occurs for multiple reasons: Having a <script /> tag that points to an HTML file instead of a JS file. Getting an HTML response from a server where JSON is expected. Having a <script /> tag that points to an incorrect path.


2 Answers

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 shouldn't.

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.

like image 150
Seth Holladay Avatar answered Sep 27 '22 21:09

Seth Holladay


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.

like image 35
kn_pavan Avatar answered Sep 27 '22 21:09

kn_pavan