微信小程序url乱码

原因
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()将变量还原。

转 https://www.cnblogs.com/yehuisir/p/10283123.html

或者 https://blog.csdn.net/wl724120268/article/details/82894710

posted @ 2020-01-16 14:55  J__F  阅读(869)  评论(0编辑  收藏  举报