微信小程序 Unexpected end of JSON input/Unexpected token o in JSON at position 1

原因

JSON.parse无法识别某些url中的特殊字符,所以报错

mistakes.js中

nextBtn:function(){

var nextData = this.data.dataNextInfo;

console.log(nextData.pop());

var nextDatas = JSON.stringify(nextData.pop())

wx.redirectTo({

url: '../mistakes1/mistakes1?nextDatas=' + encodeURIComponent(nextDatas)

})

跳转页:mistakes1.js

/**

* 生命周期函数--监听页面加载

*/

onLoad: function (options) {

var nextData = decodeURIComponent((options.nextDatas));

console.log(JSON.parse(nextData));

},

解决方案

在JSON.stringify()之后将变量使用encodeURIComponent函数处理,encodeURIComponent() 函数可把字符串作为 URI 组件进行编码。在目标页面接收时用decodeURIComponent对URI 组件进行解码,后通过JSON.parse()将变量还原。