错误详情
今天查看上线的微信小程序后台,发现了一个这样的错误:
Unexpected token in JSON at position 52;
at pages/send/send onShow function;
at api request success callback function
于是我打开调试工具查看源代码并且调试:
VM792:1 thirdScriptError
Unexpected token
in JSON at position 52;at pages/send/send onShow function;
at api request success callback function
SyntaxError: Unexpected token
in JSON at position 52
at JSON.parse (<anonymous>)
at success (http://127.0.0.1:60162/appservice/pages/send/send.js:130:35)
at http://127.0.0.1:60162/appservice/utils/bmob.js:2293:37
at wrappedResolvedCallback (http://127.0.0.1:60162/appservice/utils/bmob.js:2219:48)
at http://127.0.0.1:60162/appservice/utils/bmob.js:2171:34
at Object._.each._.forEach [as _arrayEach] (http://127.0.0.1:60162/appservice/utils/underscore.js:161:17)
at Bmob.Promise.resolve (http://127.0.0.1:60162/appservice/utils/bmob.js:2170:18)
at wrappedResolvedCallback (http://127.0.0.1:60162/appservice/utils/bmob.js:2228:37)
at http://127.0.0.1:60162/appservice/utils/bmob.js:2171:34
at Object._.each._.forEach [as _arrayEach] (http://127.0.0.1:60162/appservice/utils/underscore.js:161:17)
果不其然,小程序已然上线,这个错误一定不能忍
调试过程
通过错误信息定位到出错的代码
通过上面的报错信息:at success (http://127.0.0.1:60162/appservice/pages/send/send.js:130:35)
定位到出错的代码是在130行:var jsonB = JSON.parse(jsonA);
上网浏览查询相关资料
网上的说法不一而足,实在不知道该听信哪家之言
不过都集中在json文件不能有注释
、json字符串url有误
、json字符串有特殊字符
定位原因
排除了几种错误后还没有解决问题,有点心灰意冷,
尝试着输出json字符串中的每一个字符
51 "。"
52 "
"
53 "2"
等等,换行符确是是个特殊字符(ps,在js里面\r
是回车符,\n
是换行符,)
(在小程序中,我这个jsonA是包含用户的键盘输入信息的)
解决办法
jsonA = jsonA.replace('\n','')