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

本文解决了一个关于JSON解析的问题,当JSON.parse处理包含特殊字符的URL时会出现错误。文章提供了具体的代码示例,并介绍了解决方案:使用encodeURIComponent和decodeURIComponent来避免这类错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值