美文网首页
Url中Json数据处理

Url中Json数据处理

作者: _既白_ | 来源:发表于2018-11-27 15:35 被阅读34次

需求描述

微信授权URL,给重定向的Url 传参数,这时我们需要给state传递多个参数,就需要将多参数处理成Json字符串,如 增加 id=1,name=xiaoming

https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx5fb25c9dc619f190&redirect_uri=http://test-recycle-h5.jxypapp.com&response_type=code&scope=snsapi_base&state=#wechat_redirect

处理需求

id=1,name=xiaoming 转化为Json字符串,并将转化后Json字符串encode


let state = encodeURIComponent(JSON.stringify({
                id:'1',
                name:'xiaoming'
            })) ;

encode的state输出结果:%7B%22id%22%3D%221%22%2C%22name%22%3A%22xiaoming%22%7D

Json化输出state的结果:{"id"="1","name":"xiaoming"}

输出结果

https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx5fb25c9dc619f190&redirect_uri=http://test-recycle-h5.jxypapp.com&response_type=code&scope=snsapi_base&state=%7B%22id%22%3D%221%22%2C%22name%22%3A%22xiaoming%22%7D#wechat_redirect

取state传递的值

state值,先将params.state decode化 ,再将 decode后的数据JSON.parse` 转化为对象

let state = JSON.parse(decodeURIComponent(params.state));
 
输出结果:
 {
     id:'1',
     name:'xiaoming'
 }

相关文章

网友评论

      本文标题:Url中Json数据处理

      本文链接:https://www.haomeiwen.com/subject/ftggqqtx.html