其他分享
首页 > 其他分享> > [WEB开发]html页面向后台传递url中文乱码解决方案

[WEB开发]html页面向后台传递url中文乱码解决方案

作者:互联网

开发中常遇到页面向action或service传递url,并通过url传递中文参数问题,尤其是表单提交。而由于表单内容文本的编码是根据浏览器的规则,因此,在传递的时候常出现中文乱码的情况,以下给出解决方案:

 在js中将中文信息进行编码如url = encodeURI(url);,此时action或service得到的将%23%3E%3……此类的文本,但由于浏览器把%误认为转义字符,因此解决方案为套两层编码,如url = encodeURI(encodeURI(url));

 之后在action或是service中使用url=URLDecoder.decode(url, "UTF-8");即可完美解决中文乱码问题。此方法类似密码学中的加密和解密过程。
   let urlName = "/html/h.html?type=init&city="+ this.cityName +"&province=" + this.provinceName;
    let urls = encodeURI(encodeURI(urlName));

decodeURI(typeName)
encodeURIComponent
decodeURIComponent

标签:WEB,service,url,乱码,html,action,encodeURI
来源: https://www.cnblogs.com/linhan8888/p/15723863.html