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

微信小程序中,在转json是报错Unexpected end of JSON input/Unexpected token o in JSON at position 1

 因为JSON.parse无法识别某些url中的特殊字符,所以报错

解决方案

在JSON.stringify()之后将变量使用encodeURIComponent函数处理,encodeURIComponent() 函数可把字符串作为 URI 组件进行编码。在目标页面接收时用decodeURIComponent对URI 组件进行解码,后通过JSON.parse()将变量还原。

例如

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));

},


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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容