互联网技术 · 2024年2月18日

解决微信小程序页面跳转时传参对象数据过长的问题

小程序url传参如果是对象的话需要先用JSON.stringify()转换一下,接受页面在用JSON.parse()再转换回来。

但这样传参有一个问题,当对象数据长度过大时会报错,因为url传参时程序把过长的那段数据给截取掉了,导致数据转换回来时格式不对而报错。

这时可以再添加另一个API:encodeURIComponent(obj) 和 decodeURIComponent(options.obj),在encodeURIComponent之前要用JSON.stringify()先转换数据,decodeURIComponent之后再用JSON.parse()转换回来。

encodeURIComponent(JSON.stringify(obj))为跳转url时的转换方法。

JSON.parse(decodeURIComponent(options.obj))为接收参数页面的转换方法。

这样完美解决我在项目中遇到的问题。

OpenMagic API

Need more than content? Move into the product flow.

If you are here for model access, pricing, developer docs, or the future API console, the dedicated product path now lives on api.openmagic.ai.

登录免费注册