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 相关文章推荐
静态页面下用javascript操作ACCESS数据库(读增改删)的代码
May 14 Javascript
javascript下function声明一些小结
Dec 28 Javascript
javascript firefox不显示本地预览图片问题的解决方法
Nov 12 Javascript
setTimeout和setInterval的区别你真的了解吗?
Mar 31 Javascript
javascript使用location.search的示例
Nov 05 Javascript
javascript 事件处理示例分享
Dec 31 Javascript
深入解析JavaScript中的数字对象与字符串对象
Oct 21 Javascript
理解JavaScript中Promise的使用
Jan 18 Javascript
jQuery加载及解析XML文件的方法实例分析
Jan 22 Javascript
JS笛卡尔积算法与多重数组笛卡尔积实现方法示例
Dec 01 Javascript
详解vue使用vue-layer-mobile组件实现toast,loading效果
Aug 31 Javascript
element-ui 的el-button组件中添加自定义颜色和图标的实现方法
Oct 26 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的AES加密算法类
2015/03/12 PHP
php判断IP地址是否在多个IP段内
2020/08/18 PHP
JavaScript和CSS交互的方法汇总
2014/12/02 Javascript
JavaScript File API实现文件上传预览
2016/02/02 Javascript
jquery实现列表上下移动功能
2016/02/25 Javascript
axios学习教程全攻略
2017/03/26 Javascript
jquery实现用户登陆界面(示例讲解)
2017/09/06 jQuery
Flutter部件内部状态管理小结之实现Vue的v-model功能
2019/06/11 Javascript
Vue分页器实现原理详解
2019/06/28 Javascript
JavaScript禁止右击保存图片,禁止拖拽图片的实现代码
2020/04/28 Javascript
初步解析Python中的yield函数的用法
2015/04/03 Python
用Python生成器实现微线程编程的教程
2015/04/13 Python
简单说明Python中的装饰器的用法
2015/04/24 Python
python对url格式解析的方法
2015/05/13 Python
Python实现建立SSH连接的方法
2015/06/03 Python
python中从str中提取元素到list以及将list转换为str的方法
2018/06/26 Python
Python的iOS自动化打包实例代码
2018/11/22 Python
python实现画出e指数函数的图像
2019/11/21 Python
pytorch中tensor.expand()和tensor.expand_as()函数详解
2019/12/27 Python
pytorch下大型数据集(大型图片)的导入方式
2020/01/08 Python
可视化pytorch 模型中不同BN层的running mean曲线实例
2020/06/24 Python
OpenCV+python实现实时目标检测功能
2020/06/24 Python
北美领先的智能产品购物网站:Wellbots
2018/06/11 全球购物
JD Sports芬兰:英国领先的运动鞋和运动服饰零售商
2018/11/16 全球购物
澳大利亚办公室装修:JasonL Office Furniture
2019/06/25 全球购物
Harman Audio官方商店:购买JBL、Harman Kardon、Infinity和AKG
2019/12/05 全球购物
如何执行一个shell程序
2012/11/23 面试题
《沉香救母》教学反思
2014/04/19 职场文书
《青蛙看海》教学反思
2014/04/23 职场文书
班风口号
2014/06/18 职场文书
捐书仪式主持词
2015/07/04 职场文书
春节慰问简报
2015/07/21 职场文书
《中国机长》观后感:敬畏生命,敬畏职责
2019/11/12 职场文书
话题作文之生命的旋律
2019/12/17 职场文书
详解CSS不受控制的position fixed
2021/05/25 HTML / CSS
《英雄联盟》2022日蚀、月蚀皮肤演示 黑潮亚索曝光
2022/04/13 其他游戏