首页 > Web开发 > 详细

为什么要使用encodeuricomponent?

时间:2021-09-21 16:01:53      阅读:31      评论:0      收藏:0      [点我收藏+]

为什么要使用encodeuricomponent?

1、encodeuricomponent

可把字符串作为 URI 组件进行编码。

该方法不会对 ASCII 字母和数字进行编码,也不会对这些 ASCII 标点符号进行编码: - _ . ! ~ * ’ ( ) 。

其他字符(比如 :;/???&=+$,# 这些用于分隔 URI 组件的标点符号),都是由一个或多个十六进制的转义序列替换的。

2、encodeuricomponent什么时候使用:用于url作为参数传递的场景中使用
url当作参数传递的时候,当参数出现空格这样的特殊字段,后台只可以读取到空格前的内容,后面内容丢失,造成数据读取失败,但是如果用encodeURIComponent(),则这些特殊字符进行转义,这样后台就可以成功读取了,所以encodeURIComponent()用于url作为参数传递的场景中使用。

2、decodeURIComponent
decodeURIComponent() 函数可对 encodeURIComponent() 函数编码的 URI 进行解码。

例如:

   传参数页面

var res = encodeURIComponent(JSON.stringify(res))
console.log(res)
wx.navigateTo({ url: ‘/pages/addaddress/main?res=‘+res });

接收数据的页面:

mounted () {
    this.openId = wx.getStorageSync(‘openId‘)||‘‘;
//看是从哪个页面跳转过来(获取跳转过来的信息)
if(this.$root.$mp.query.res){
this.res = JSON.parse(decodeURIComponent(this.$root.$mp.query.res))
console.log(this.res)
console.log(typeof this.res)
this.userName = this.res.userName;
this.telNumber = this.res.telNumber;
this.address = `${this.res.provinceName} ${this.res.cityName} ${this.res.countyName}`;
this.detailadress = this.res.detailInfo;
}
},

为什么要使用encodeuricomponent?

原文:https://www.cnblogs.com/webSnow/p/15307306.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!