微信小程序跳转传参报错:Unexpected end of JSON input

微信小程序跳转传参报错:Unexpected end of JSON input

解决办法
使用encodeURIComponent编码

A页面

	//自定义跳转方法
	function navigate(category, uuid, index, allInfo) {
		    wx.navigateTo({
		        url: `/pages/component/insidePages/${category}/${category}?category=${category}&uuid=${uuid}&index=${index}&allInfo=${allInfo}`,
		    })
		    
    //跳转详情页
    _jumpDetails(e) {
     console.log(e.currentTarget.dataset);
     let type = e.currentTarget.dataset.type
     let category = e.currentTarget.dataset.category
     let uuid = e.currentTarget.dataset.uuid
     let index = e.currentTarget.dataset.index
     //allInfo是数组 跳转携带数组需要转换数据格式 JSON.stringify()
     let allInfo = encodeURIComponent(JSON.stringify(this.data[`${category}Index`]))
     console.log(allInfo);
     
     if (type == 1) {
        utils.navigate("anime", uuid, index)
     } else {
     	//跳转 携带四个参数 allInfo是数组
        utils.navigate(category, uuid, index, allInfo)
     }

B页面

onLoad(options){
	//此处接收也需要转换格式
    let allInfo = JSON.parse(decodeURIComponent(options.allInfo))
}

总结

	1.传参数时先将数组转化为JSON数据格式JSON.stringify(),再编码encodeURIComponent()
	2.接收时先编码decodeURIComponent(),再转换格式JSON.parse()
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值