Javascript 未捕获的语法错误:JSON 中的意外标记 u 在位置 0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46613243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:31:14  来源:igfitidea点击:

Uncaught SyntaxError: Unexpected token u in JSON at position 0

javascriptjsonsyntax-errormagento2

提问by Howli

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.

我使用的是一页结帐扩展程序,但是当我禁用它时,错误仍然显示。我认为这可能与产品页面上的评论有关(因为我将评论移出了选项卡),但撤消该更改并不能修复产品页面上的错误。

回答by Seth Holladay

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.

换句话说,您的应用程序正在尝试解析undefined无效的 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.

第二个常见原因首先是无法接收 JSON,这可能是由客户端脚本导致的,这些脚本忽略错误并在不应该发送请求时发送请求。

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

确保您的服务器端和客户端脚本都在严格模式下运行,并使用ESLint 对它们进行lint。这会给你很好的信心,没有错别字。

回答by kn_pavan

As @Seth Holladay @MinusFour commented, you are parsing an undefinedvariable.
Try adding an ifcondition before doing the parse.

正如@Seth Holladay @MinusFour 评论的那样,您正在解析一个undefined变量。在进行解析之前
尝试添加if条件。

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

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

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

注意:这只是一个undefined案例检查。任何其他解析问题仍然需要处理。

回答by Sveta Oksen

For me, that happened because I had an empty component in my page -

对我来说,这是因为我的页面中有一个空组件 -

<script type="text/x-magento-init">
   {
   ".page.messages": {
       "Magento_Ui/js/core/app": []        
      }
   }

Deleting this piece of code resolved the issue.

删除这段代码解决了这个问题。

回答by Mekanic

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

我有这个问题 2 天了,让我告诉你我是如何解决它的。

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.responseto this.responseText.

所以我真正做的就是更改 this.responsethis.responseText.

回答by Ankur

This is due to the interfering messagesthat 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 cookieqfor managing cookies, or may be cartwirean e-com integration service.

这是由于messages页面上的干扰。页面上有多个框架,它们使用窗口消息事件和对象与页面进行通信。其中很少有第三方服务,例如cookieq用于管理 cookie 的服务,或者可能是cartwire电子商务集成服务。

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

您需要处理 onmessage 事件以检查消息来自何处,然后相应地解析 JSON。

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

我遇到了类似的问题,其中一个集成传递一个 JSON 对象,另一个传递一个以开头的字符串 u