Uncaught SyntaxError: Unexpected token u in JSON at position 0
Uncaught SyntaxError: Unexpected token u in JSON at position 0
仅在结帐和个别产品页面上,我在控制台日志中收到以下错误:
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)
我正在使用单页结帐扩展程序,但是当我禁用它时错误仍然显示。我认为这可能与产品页面上的评论有关(因为我将评论移出选项卡),但撤消更改并没有修复产品页面上的错误。
在控制台试试这个:
JSON.parse(undefined)
以下是您将获得的内容:
Uncaught SyntaxError: Unexpected token u in JSON at position 0
at JSON.parse (<anonymous>)
at <anonymous>:1:6
换句话说,您的应用正在尝试解析 undefined
,这是无效的 JSON。
造成这种情况的常见原因有两个。首先是您可能引用了一个不存在的 属性(如果不是在严格模式下,甚至是一个不存在的变量)。
window.foobar = '{"some":"data"}';
JSON.parse(window.foobarn) // oops, misspelled!
第二个常见原因是首先未能收到 JSON,这可能是由于客户端脚本忽略了错误并在不应发送请求时发送了请求。
确保您的服务器端和客户端脚本都是 运行 in strict mode and lint them using ESLint。这将使您非常有信心没有拼写错误。
这是由于页面上的干扰 messages
造成的。页面上有多个框架使用 window 消息事件和对象与页面通信。其中很少有第三方服务,例如 cookieq
用于管理 cookie,或者可能是 cartwire
电子商务集成服务。
您需要处理 onmessage 事件以检查消息来自何处,然后相应地解析 JSON。
我遇到了类似的问题,其中一个集成传递了一个 JSON 对象,另一个传递了一个以 u
开头的字符串
正如@Seth Holladay @MinusFour 评论的那样,您正在解析一个 undefined
变量。
在进行解析之前尝试添加一个 if
条件。
if (typeof test1 !== 'undefined') {
test2 = JSON.parse(test1);
}
注意:这只是对 undefined
案例的检查。任何其他解析问题仍然需要处理。
对我来说,这是因为我的页面中有一个空组件 -
<script type="text/x-magento-init">
{
".page.messages": {
"Magento_Ui/js/core/app": []
}
}
删除这段代码解决了这个问题。
我已经遇到这个问题 2 天了,让我告诉你我是如何解决它的。
这是我遇到错误时代码的样子:
request.onload = function() {
// This is where we begin accessing the Json
let data = JSON.parse(this.response);
console.log(data)
}
这是我为了得到我想要的结果所做的更改:
request.onload = function() {
// This is where we begin accessing the Json
let data = JSON.parse(this.responseText);
console.log(data)
}
所以我真正做的就是改变
this.response
到 this.responseText
。
localStorage.clear()
这将清除存储的数据。然后刷新,事情应该开始工作了。
您的应用正在尝试解析未定义的 JSON 网络令牌。此类故障可能是由于本地存储的错误使用而导致的。尝试清除您的本地存储。
Google Chrome 的示例:
- F12
- 申请
- 本地存储
- 全部清除
如果您得到 Uncaught SyntaxError: Unexpected token u in JSON at position 0
,那么您可以进行以下快速检查:
jsObj = JSON.parse(data)
数据 - 检查数据是否 未定义 或未定义
data - 检查数据是否有效 JSON string or not
数据 - 检查数据是否被不需要的空格损坏
data = data.trim(); // remove the unwanted whitespace
jsObj = JSON.parse(data);
data - 检查接收到的数据是否使用了正确的编码格式('utf8', 'utf16le', 'ucs2')
fs.readFile(file, 'utf8', function(err, data) { ... });
fs.readFile(file, 'utf16le', function(err, data) { ... }); // le - little endian
fs.readFile(file, 'ucs2', function(err, data) { ... }); // kind of 'utf16le'
仅在结帐和个别产品页面上,我在控制台日志中收到以下错误:
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)
我正在使用单页结帐扩展程序,但是当我禁用它时错误仍然显示。我认为这可能与产品页面上的评论有关(因为我将评论移出选项卡),但撤消更改并没有修复产品页面上的错误。
在控制台试试这个:
JSON.parse(undefined)
以下是您将获得的内容:
Uncaught SyntaxError: Unexpected token u in JSON at position 0
at JSON.parse (<anonymous>)
at <anonymous>:1:6
换句话说,您的应用正在尝试解析 undefined
,这是无效的 JSON。
造成这种情况的常见原因有两个。首先是您可能引用了一个不存在的 属性(如果不是在严格模式下,甚至是一个不存在的变量)。
window.foobar = '{"some":"data"}';
JSON.parse(window.foobarn) // oops, misspelled!
第二个常见原因是首先未能收到 JSON,这可能是由于客户端脚本忽略了错误并在不应发送请求时发送了请求。
确保您的服务器端和客户端脚本都是 运行 in strict mode and lint them using ESLint。这将使您非常有信心没有拼写错误。
这是由于页面上的干扰 messages
造成的。页面上有多个框架使用 window 消息事件和对象与页面通信。其中很少有第三方服务,例如 cookieq
用于管理 cookie,或者可能是 cartwire
电子商务集成服务。
您需要处理 onmessage 事件以检查消息来自何处,然后相应地解析 JSON。
我遇到了类似的问题,其中一个集成传递了一个 JSON 对象,另一个传递了一个以 u
正如@Seth Holladay @MinusFour 评论的那样,您正在解析一个 undefined
变量。
在进行解析之前尝试添加一个 if
条件。
if (typeof test1 !== 'undefined') {
test2 = JSON.parse(test1);
}
注意:这只是对 undefined
案例的检查。任何其他解析问题仍然需要处理。
对我来说,这是因为我的页面中有一个空组件 -
<script type="text/x-magento-init">
{
".page.messages": {
"Magento_Ui/js/core/app": []
}
}
删除这段代码解决了这个问题。
我已经遇到这个问题 2 天了,让我告诉你我是如何解决它的。
这是我遇到错误时代码的样子:
request.onload = function() {
// This is where we begin accessing the Json
let data = JSON.parse(this.response);
console.log(data)
}
这是我为了得到我想要的结果所做的更改:
request.onload = function() {
// This is where we begin accessing the Json
let data = JSON.parse(this.responseText);
console.log(data)
}
所以我真正做的就是改变
this.response
到 this.responseText
。
localStorage.clear()
这将清除存储的数据。然后刷新,事情应该开始工作了。
您的应用正在尝试解析未定义的 JSON 网络令牌。此类故障可能是由于本地存储的错误使用而导致的。尝试清除您的本地存储。
Google Chrome 的示例:
- F12
- 申请
- 本地存储
- 全部清除
如果您得到 Uncaught SyntaxError: Unexpected token u in JSON at position 0
,那么您可以进行以下快速检查:
jsObj = JSON.parse(data)
数据 - 检查数据是否 未定义 或未定义
data - 检查数据是否有效 JSON string or not
数据 - 检查数据是否被不需要的空格损坏
data = data.trim(); // remove the unwanted whitespace jsObj = JSON.parse(data);
data - 检查接收到的数据是否使用了正确的编码格式('utf8', 'utf16le', 'ucs2')
fs.readFile(file, 'utf8', function(err, data) { ... }); fs.readFile(file, 'utf16le', function(err, data) { ... }); // le - little endian fs.readFile(file, 'ucs2', function(err, data) { ... }); // kind of 'utf16le'