微信小程序中,在轉(zhuǎn)json是報錯Unexpected end of JSON input/Unexpected token o in JSON at position 1
?因為JSON.parse無法識別某些url中的特殊字符,所以報錯
解決方案
在JSON.stringify()之后將變量使用encodeURIComponent函數(shù)處理锨能,encodeURIComponent() 函數(shù)可把字符串作為 URI 組件進行編碼位衩。在目標頁面接收時用decodeURIComponent對URI 組件進行解碼篷朵,后通過JSON.parse()將變量還原堂竟。
例如
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)
})
跳轉(zhuǎn)頁:mistakes1.js
/**
* 生命周期函數(shù)--監(jiān)聽頁面加載
*/
onLoad: function (options) {
var nextData = decodeURIComponent((options.nextDatas));
console.log(JSON.parse(nextData));
},