小程序传递对象参数发现报错

现象

在小程序页面传递对象参数时,发现界面报错

SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>)

分析

通过分析发现JSON.parse在解析含有特殊字符的对象时,会报错

特殊字符: 英文(半角)状态的 单引号' 双引号" 斜杠反斜杠/ and符号& 大于号> 小于号< 空格 等.

解决办法

编码解码 (具体使用参照 小程序传递带?参数时,发现将?之后的参数截取掉了

// 编码
encodeURIComponent()
// 解码
decodeURIComponent()

将所有的特殊字符进行转义处理

objStr.replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/‘/g,"&#39;").replace(/</g,"&lt;").replace(/>/g,"&gt;")

 

posted @ 2023-07-21 16:46  南无、  阅读(89)  评论(0)    收藏  举报