decodeURIComponent中文转码问题

编程入门 行业动态 更新时间:2024-10-22 10:59:09

decodeURIComponent<a href=https://www.elefans.com/category/jswz/34/1769975.html style=中文转码问题"/>

decodeURIComponent中文转码问题

不知道大家有没有这样的困扰,汉字转码问题,因为浏览器会对汉字自动转码,并且某些302之后可能又转码了一次,并且有的值在各种页面的url上传递,传来传去,前端可能也不知道究竟转码了几次,然后后端就蒙蔽了,明明解码了呀,balabala。其实个人觉得最好尽量少使用汉字作为参数传递。不过要是必须用也是有办法的,那就是循环调用decodeURIComponent,直到转换完的和上一次的值是一样的为止,使用decodeURIComponent时可能会报错,比如转码8%,意思是百分之8,但是decodeURIComponent对百分号敏感,所以需要用try catch 捕获异常,当error发生时,也停止继续decodeURIComponent,具体代码如如下:

function decode(pre) {function isEqual(pre, cur) {return pre === cur}function decodeURIComponent(str) {try {return window.decodeURIComponent(str)} catch (err) {return str}}let cur = decodeURIComponent(pre)while (!isEqual(pre, cur)) {pre = curcur = decodeURIComponent(cur)}return cur
}

更多推荐

decodeURIComponent中文转码问题

本文发布于:2024-03-05 04:30:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1711317.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中文   decodeURIComponent

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!