JS URL传中文参数引发的乱码问题


Posted in Javascript onSeptember 02, 2009

解决方法如下:

1、在JS里对中文参数进行两次转码

var login_name = document.getElementById("loginname").value; 

login_name = encodeURI(login_name); 

login_name = encodeURI(login_name);

2、在服务器端对参数进行解码
String loginName = ParamUtil.getString(request, "login_name"); 

loginName = java.net.URLDecoder.decode(loginName,"UTF-8");

在使用url进行参数传递时,经常会传递一些中文名的参数或URL地址,在后台处理时会发生转换错误。在有些传递页面使用GB2312,而在接收页面使用UTF8,这样接收到的参数就可能会与原来发生不一致。使用服务器端的urlEncode函数编码的URL,与使用客户端javascript的encodeURI函数编码的URL,结果就不一样。

javaScript中的编码方法:

escape() 方法:
采用ISO Latin字符集对指定的字符串进行编码。所有的空格符、标点符号、特殊字符以及其他非ASCII字符都将被转化成%xx格式的字符编码(xx等于该字符在字符集表里面的编码的16进制数字)。比如,空格符对应的编码是%20。unescape方法与此相反。不会被此方法编码的字符: @ * / +

如果是gb2312编码的可以使用escape,不能用encodeURIComponent,要不会乱码。

escape的使用方法:https://3water.com/w3school/jsref/jsref_escape.htm

英文解释:MSDN JScript Reference: The escape method returns a string value (in Unicode format) that contains the contents of [the argument]. All spaces, punctuation, accented characters, and any other non-ASCII characters are replaced with %xx encoding, where xx is equivalent to the hexadecimal number representing the character. For example, a space is returned as "%20."
Edge Core Javascript Guide: The escape and unescape functions let you encode and decode strings. The escape function returns the hexadecimal encoding of an argument in the ISO Latin character set. The unescape function returns the ASCII string for the specified hexadecimal encoding value.

encodeURI() 方法:把URI字符串采用UTF-8编码格式转化成escape格式的字符串。不会被此方法编码的字符:! @ # $& * ( ) = : / ; ? + '

英文解释:MSDN JScript Reference: The encodeURI method returns an encoded URI. If you pass the result to decodeURI, the original string is returned. The encodeURI method does not encode the following characters: ":", "/", ";", and "?". Use encodeURIComponent to encode these characters. Edge Core Javascript Guide: Encodes a Uniform Resource Identifier (URI) by replacing each instance of certain characters by one, two, or three escape sequences representing the UTF-8 encoding of the character

encodeURIComponent() 方法:把URI字符串采用UTF-8编码格式转化成escape格式的字符串。与encodeURI()相比,这个方法将对更多的字符进行编码,比如 / 等字符。所以如果字符串里面包含了URI的几个部分的话,不能用这个方法来进行编码,否则 / 字符被编码之后URL将显示错误。不会被此方法编码的字符:! * ( )

英文解释:MSDN JScript Reference: The encodeURIComponent method returns an encoded URI. If you pass the result to decodeURIComponent, the original string is returned. Because the encodeURIComponent method encodes all characters, be careful if the string represents a path such as /folder1/folder2/default.html. The slash characters will be encoded and will not be valid if sent as a request to a web server. Use the encodeURI method if the string contains more than a single URI component. Mozilla Developer Core Javascript Guide: Encodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two, or three escape sequences representing the UTF-8 encoding of the character.

因此,对于中文字符串来说,如果不希望把字符串编码格式转化成UTF-8格式的(比如原页面和目标页面的charset是一致的时候),只需要使用escape。如果你的页面是GB2312或者其他的编码,而接受参数的页面是UTF-8编码的,就要采用encodeURI或者encodeURIComponent。

另外,encodeURI/encodeURIComponent是在javascript1.5之后引进的,escape则在javascript1.0版本就有。

英文注释:The escape() method does not encode the + character which is interpreted as a space on the server side as well as generated by forms with spaces in their fields. Due to this shortcoming, you should avoid use of escape() whenever possible. The best alternative is usually encodeURIComponent().Use of the encodeURI() method is a bit more specialized than escape() in that it encodes for URIs [REF] as opposed to the querystring, which is part of a URL. Use this method when you need to encode a string to be used for any resource that uses URIs and needs certain characters to remain un-encoded. Note that this method does not encode the ' character, as it is a valid character within URIs.Lastly, the encodeURIComponent() method should be used in most cases when encoding a single component of a URI. This method will encode certain chars that would normally be recognized as special chars for URIs so that many components may be included. Note that this method does not encode the ' character, as it is a valid character within URIs.

Javascript 相关文章推荐
兼容Mozilla必须知道的知识。
Jan 09 Javascript
js子页面获取父页面数据示例
May 15 Javascript
浅析使用BootStrap TreeView插件实现灵活配置快递模板
Nov 28 Javascript
webpack打包后直接访问页面图片路径错误的解决方法
Jun 17 Javascript
Angular指令之restict匹配模式的详解
Jul 27 Javascript
JavaScript学习笔记之惰性函数示例详解
Aug 27 Javascript
AngularJS中controller控制器继承的使用方法
Nov 03 Javascript
深入浅析Vue中的Prop
Jun 10 Javascript
详解vue axios二次封装
Jul 22 Javascript
vue-cli3+typescript新建一个项目的思路分析
Aug 06 Javascript
js实现随机div颜色位置 类似满天星效果
Oct 24 Javascript
JavaScript中的this妙用实例分析
May 09 Javascript
FF IE兼容性的修改小结
Sep 02 #Javascript
js 获取浏览器高度和宽度值(多浏览器)
Sep 02 #Javascript
获取URL地址中的文件名和参数的javascript代码
Sep 02 #Javascript
Javascript 判断函数类型完美解决方案
Sep 02 #Javascript
javascript 控制 html元素 显示/隐藏实现代码
Sep 01 #Javascript
jsTree树控件(基于jQuery, 超强悍)[推荐]
Sep 01 #Javascript
JavaScript 继承详解 第一篇
Aug 30 #Javascript
You might like
PHP 如何向 MySQL 发送数据
2006/10/09 PHP
使用php+swoole对client数据实时更新(一)
2016/01/07 PHP
Laravel中七个非常有用但很少人知道的Carbon方法
2017/09/21 PHP
浅谈PHP各环境下的伪静态配置
2019/03/13 PHP
PHP生成随机码的思路与方法实例探索
2019/04/11 PHP
JavaScript监测ActiveX控件是否已经安装过的代码
2008/09/02 Javascript
javascript不同页面传值的改进版
2008/09/30 Javascript
javascript Window及document对象详细整理
2011/01/12 Javascript
window.showModalDialog参数传递中含有特殊字符的处理方法
2013/06/06 Javascript
深入理解javascript中defer的作用
2013/12/11 Javascript
JS将光标聚焦在文本最后的实现代码
2014/03/28 Javascript
NodeJS中利用Promise来封装异步函数
2015/02/25 NodeJs
浅析JavaScript 调试方法和技巧
2015/10/22 Javascript
详解React Native开源时间日期选择器组件(react-native-datetime)
2017/09/13 Javascript
详解Angular中通过$location获取地址栏的参数
2018/08/02 Javascript
vue+iview 兼容IE11浏览器的实现方法
2019/01/07 Javascript
Windows上node.js的多版本管理工具用法实例分析
2019/11/06 Javascript
JS实现TITLE悬停长久显示效果完整示例
2020/02/11 Javascript
原生js实现日历效果
2020/03/02 Javascript
vue select 获取value和lable操作
2020/08/28 Javascript
[47:04]EG vs RNG 2019国际邀请赛小组赛 BO2 第二场 8.16
2019/08/18 DOTA
python根据文件大小打log日志
2014/10/09 Python
快速解决安装python没有scripts文件夹的问题
2018/04/03 Python
python去掉 unicode 字符串前面的u方法
2018/10/21 Python
pycharm配置git(图文教程)
2019/08/16 Python
Python中join()函数多种操作代码实例
2020/01/13 Python
详解django使用include无法跳转的解决方法
2020/03/19 Python
全球最大的网上自行车商店:Chain Reaction Cycles
2016/12/02 全球购物
JS原生实现轮播图的几种方法
2021/03/23 Javascript
参观监狱心得体会
2014/01/02 职场文书
高二美术教学反思
2014/01/14 职场文书
研修第一天随笔感言
2014/02/15 职场文书
宣传部部长竞选演讲稿
2014/04/26 职场文书
保险专业求职信
2014/07/07 职场文书
六一儿童节主持开场白
2015/05/28 职场文书
2015秋季开学典礼新闻稿
2015/07/17 职场文书