
WAServiceMainContext.js:2 SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>)
若对象的参数或数组的元素中遇到地址,地址中包括?、&这些特殊符号时,就比如我们经常用的页面传参,或者使用getStorageSync使用缓存的时候,需要转成josn格式
·
记一次小程序传值错误

1.报错示范:
wx.navigateTo({
url: '/pages/userinfo/userinfo?item=' + JSON.stringify(e.currentTarget.dataset.item)
});
if (options && options.item) {
this.setData({
item: JSON.parse(options.item),
});
}
1.正确示范:
wx.navigateTo({
url: '/pages/userinfo/userinfo?item=' + encodeURIComponent(JSON.stringify(e.currentTarget.dataset.item))
});
this.setData({
item: JSON.parse(decodeURIComponent(options.item)),
});
希望能帮到你 这个直接复制运行是正确的 像之前我看了一篇是这样的
const item = JSON.stringify(row)
uni.navigateTo({
url:'/announcement/pages/information/index?item=' + encodeURIComponent(item)
})
onLoad(option) {
this.informationType = JSON.parse(decodeURIComponent(option.item))
this.communityId = uni.getStorageSync('communityId')
this.getList()
},
我的这个跟他的区别是 如果你传的item是对象就用我那个 如果直接就是字符串就用最下面这个 就是我一开始在encodeURIComponent编码之前将对象转换成了字符串。
3.出现这个错可能的原因:
若对象的参数或数组的元素中遇到地址,地址中包括?、&这些特殊符号时,对象/数组先要通过JSON.stringify转化为字符串再通过encodeURIComponent编码,接收时,先通过decodeURIComponent解码再通过JSON.parse转换为JSON格式的对象/数组
就比如我们经常用的页面传参,或者使用getStorageSync使用缓存的时候,需要转成josn格式
更多推荐
所有评论(0)