微信小程序 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()將變量還原。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM