WeChat Mini Program-The content of the parameter field of the page with parameters is too long or contains special characters ("?", "&" or "="), resulting in interception or failure

Preface

In WeChat Mini Programs, wx.navigateTo({})it is common to use APIs to carry parameters to jump pages, but today there is a requirement to carry a complex data. It is an array with N objects nested in it, and there is an array in the object. So I need to use JSON.stringifyan object into a string, and then pass in the past.

My scenario: where imageproperty contains links to articles, has led to JSON.parsedecoding failure, because they were intercepted.

solution

With encodeURIComponent()transcoding, and then in the target page decodeURIComponent()decoding.

/*
* [发送参数]
* 1. 将对象解析为字符串
* 2. 把字符串作为 URI 组件进行编码
*/

wx.navigateTo({
    
    
     url: '/pages/index?data=' + encodeURIComponent(JSON.stringify(object)),
})
/*
* [接收参数]
* 1. 将字符串解析为对象
* 2. 把字符串作为 URI 组件进行解码
*/

this.setData({
    
    
	data: JSON.parse(decodeURIComponent(options.data))
})

Guess you like

Origin blog.csdn.net/weixin_44198965/article/details/108873557