Uncaught SyntaxError: Unexpected token u in JSON at position 0

前端 未结 7 1386
[愿得一人]
[愿得一人] 2020-12-03 00:11

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          


        
相关标签:
7条回答
  • 2020-12-03 00:56

    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.

    0 讨论(0)
  • 2020-12-03 00:58

    Your app is attempting to parse the undefined JSON web token. Such malfunction may occur due to the wrong usage of the local storage. Try to clear your local storage.

    Example for Google Chrome:

    1. F12
    2. Application
    3. Local Storage
    4. Clear All
    0 讨论(0)
  • 2020-12-03 01:01
    localStorage.clear()
    

    That'll clear the stored data. Then refresh and things should start to work.

    0 讨论(0)
  • 2020-12-03 01:08

    I had this issue for 2 days, let me show you how I fixed it.

    This was how the code looked when I was getting the error:

    request.onload = function() {
        // This is where we begin accessing the Json
        let data = JSON.parse(this.response);
        console.log(data)
    }
    

    This is what I changed to get the result I wanted:

    request.onload = function() {
        // This is where we begin accessing the Json
        let data = JSON.parse(this.responseText);
        console.log(data)
    }
    

    So all I really did was change this.response to this.responseText.

    0 讨论(0)
  • 2020-12-03 01:10

    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.

    0 讨论(0)
  • 2020-12-03 01:12

    This is due to the interfering messages that come on to the page. There are multiple frames on the page which communicate with the page using window message event and object. few of them can be third party services like cookieq for managing cookies, or may be cartwire an e-com integration service.

    You need to handle the onmessage event to check from where the messages are coming, and then parse the JSON accordingly.

    I faced a similar problem, where one of the integration was passing a JSON object and other was passing a string starting with u

    0 讨论(0)
提交回复
热议问题