Uncaught SyntaxError: Unexpected token E in JSON at position 0 at JSON.parse ()
程序员文章站
2022-05-30 18:24:09
...
当直接将返回值JSON.parse(),并且使用这个值去取值,报这个Uncaught SyntaxError: Unexpected token E in JSON at position 0 at JSON.parse ()。
JSON.parse()里的参数只能是string类型,但是在下面的代码中,返回的不是正常的值,不是string,所以会报错。
var resData = JSON.parse(resp);
var code= resData['code'];
var msg= resData['msg'];
alert(msg)
所以在使用这个值的时候,需要加一层判断:
let msg;
let dataType = typeof resp == 'string';
if (dataType) {
var resData = JSON.parse(resp);
var code= resData['code'];
msg= resData['msg'];
alert(msg)
} else {
let errormsg = result.msg;
msg = errormsg.address + ':' + errormsg.port + ' ' + '请检查访问地址后重试.';
alert(msg)
}
这样就不报错了
参考:
https://blog.csdn.net/user_zpa/article/details/80280972
推荐阅读
-
vue 报错Error in mounted hook: "SyntaxError: Unexpected token u in JSON at position 0"
-
报错: Error in created hook: “SyntaxError: Unexpected token u in JSON at position 0
-
flutter报错解决 Error: FormatException: SyntaxError: Unexpected token m in JSON at position 1
-
JSOIN.parse()报错:Uncaught SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse (
) -
JSON字符格式报错:VM40:1 Uncaught SyntaxError: Unexpected token E in JSON at position 0
-
SyntaxError: Unexpected token R in JSON at position 0 报错的解决方法
-
报错 SyntaxError: Unexpected token R in JSON at position 0 at JSON.parse (<anonymous>)的解决方法
-
Unexpected token c in JSON at position 0 报错信息及解决
-
Uncaught SyntaxError: Unexpected token E in JSON at position 0 at JSON.parse ()
-
解决报错:Uncaught SyntaxError: Unexpected token “xxx” in JSON at position 0