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操作select方法大全[新增、修改、删除、选中、清空、判断存在等]
Sep 26 Javascript
基于Jquery实现表格动态分页实现代码
Jun 21 Javascript
基于jquery的滚动条滚动固定div(附演示下载)
Oct 29 Javascript
Jquery多选下拉列表插件jquery multiselect功能介绍及使用
May 24 Javascript
javascript中AJAX用法实例分析
Jan 30 Javascript
JavaScript中对DOM节点的访问、创建、修改、删除
Nov 16 Javascript
JavaScript实现通过select标签跳转网页的方法
Sep 29 Javascript
jQuery实现图片滑动效果
Mar 08 Javascript
jQuery实现分页功能(含ajax请求、后台数据、附完整demo)
Apr 03 jQuery
JS高阶函数原理与用法实例分析
Jan 15 Javascript
原生JS实现动态添加新元素、删除元素方法
May 05 Javascript
jquery简易手风琴插件的封装
Oct 13 jQuery
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
建立动态的WML站点(三)
2006/10/09 PHP
基于PHP+Ajax实现表单验证的详解
2013/06/25 PHP
ThinkPHP简单使用memcache缓存的方法
2016/11/15 PHP
PHP大文件分割上传 PHP分片上传
2017/08/28 PHP
PHP使用XMLWriter读写xml文件操作详解
2018/07/31 PHP
CSS+Table图文混排中实现文本自适应图片宽度(超简单+跨所有浏览器)
2009/02/14 Javascript
jQuery中使用了document和window哪些属性和方法小结
2011/09/13 Javascript
jquery选择器、属性设置用法经验总结
2013/09/08 Javascript
JS控制图片等比例缩放的示例代码
2013/12/24 Javascript
js+css3实现旋转效果
2017/01/20 Javascript
vue打包后显示空白正确处理方法
2017/11/01 Javascript
Vue中render方法的使用详解
2018/01/26 Javascript
vue.js 实现评价五角星组件的实例代码
2018/08/13 Javascript
vue2.0 + ele的循环表单及验证字段方法
2018/09/18 Javascript
JavaScript之解构赋值的理解
2019/01/30 Javascript
vue.js 打包时出现空白页和路径错误问题及解决方法
2019/06/26 Javascript
图解NodeJS实现登录注册功能
2019/09/16 NodeJs
微信小程序订阅消息(java后端实现)开发
2020/06/01 Javascript
视觉直观感受若干常用排序算法
2017/04/13 Python
python 读取txt中每行数据,并且保存到excel中的实例
2018/04/29 Python
Python学习笔记之视频人脸检测识别实例教程
2019/03/06 Python
Python数据可视化实现正态分布(高斯分布)
2019/08/21 Python
用sqlalchemy构建Django连接池的实例
2019/08/29 Python
Python zip函数打包元素实例解析
2019/12/11 Python
pytorch 修改预训练model实例
2020/01/18 Python
python中for in的用法详解
2020/04/17 Python
解决Pytorch自定义层出现多Variable共享内存错误问题
2020/06/28 Python
Linux系统下升级pip的完整步骤
2021/01/31 Python
豪华床上用品、床单和浴室必需品:Peacock Alley
2019/09/04 全球购物
大学本科生的个人自我评价
2013/12/09 职场文书
关于圣诞节的广播稿
2014/01/26 职场文书
政治学专业毕业生求职信
2014/08/11 职场文书
2015年度物业公司工作总结
2015/04/27 职场文书
2015年小学教科研工作总结
2015/07/20 职场文书
2016年暑假家长对孩子评语
2015/12/01 职场文书
golang中的空接口使用详解
2021/03/30 Python