业务场景:
微信小程序中A页面带参数跳转B页面
A页面
toDetail(e) {
const index = e.currentTarget.dataset.index;
let card = this.data.cardList[index];
card = JSON.stringify(card);
wx.navigateTo({
url: `../cardDetails/cardDetails?card=${card}`,
})
},
B页面
onLoad: function (options) {
let card = options.card;
if (card) {
let obj = JSON.parse(card);
this.setData({
card: obj
})
}
}
由于card
数据比较大,导致JSON解析的时候报错:Unexpected end of JSON input
。原因可能因为是数据太大,也可能是#等特殊字符截断(card数据里有#符号)。
问题解决:用encodeURIComponent
toDetail(e) {
const index = e.currentTarget.dataset.index;
let card = this.data.cardList[index];
card = encodeURIComponent(JSON.stringify(card));
wx.navigateTo({
url: `../cardDetails/cardDetails?card=${card}`,
})
}
onLoad: function (options) {
let card = options.card;
if (card) {
card = decodeURIComponent(card);
let obj = JSON.parse(card);
this.setData({
card: obj
})
}
}