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 相关文章推荐
jQuery select控制插件
Aug 17 Javascript
在myeclipse中如何加入jquery代码提示功能
Jun 03 Javascript
基于jQuery实现最基本的淡入淡出效果实例
Feb 02 Javascript
jquery中change()用法实例分析
Feb 06 Javascript
js实现不提交表单获取单选按钮值的方法
Aug 21 Javascript
Vue.js中数组变动的检测详解
Oct 12 Javascript
vue 数组和对象不能直接赋值情况和解决方法(推荐)
Oct 25 Javascript
基于jQuery实现定位导航位置效果
Nov 15 jQuery
JS实现的哈夫曼编码示例【原始版与修改版】
Apr 22 Javascript
ES6知识点整理之函数数组参数的默认值及其解构应用示例
Apr 17 Javascript
小程序自定义模板实现吸顶功能
Jan 08 Javascript
基于vue和bootstrap实现简单留言板功能
May 30 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 Class&Object -- 解析PHP实现二叉树
2013/06/25 PHP
PHP中SimpleXML函数用法分析
2014/11/26 PHP
PHP图片加水印实现方法
2016/05/06 PHP
PHP Socket网络操作类定义与用法示例
2017/08/30 PHP
laravel中短信发送验证码的实现方法
2018/04/25 PHP
PHP 对象接口简单实现方法示例
2020/04/13 PHP
javascript 表格排序和表头浮动效果(扩展SortTable)
2009/04/07 Javascript
jquery实现select选中行、列合计示例
2014/04/25 Javascript
用js格式化金额可设置保留的小数位数
2014/05/09 Javascript
javascript中的作用域和闭包详解
2016/01/13 Javascript
在 Angular2 中实现自定义校验指令(确认密码)的方法
2017/01/23 Javascript
Ionic项目中Native Camera的使用方法
2017/06/07 Javascript
Angular4 ElementRef的应用
2018/02/26 Javascript
使用Sonarqube扫描Javascript代码的示例
2018/12/26 Javascript
vue项目中实现的微信分享功能示例
2019/01/21 Javascript
vue-test-utils初使用详解
2019/05/23 Javascript
实例分析JS中的相等性判断===、 ==和Object.is()
2019/11/17 Javascript
vue实现图片按比例缩放问题操作
2020/08/11 Javascript
微信小程序实现转盘抽奖
2020/09/21 Javascript
原生js实现购物车功能
2020/09/23 Javascript
Python使用Flask框架获取当前查询参数的方法
2015/03/21 Python
详细讲解用Python发送SMTP邮件的教程
2015/04/29 Python
Python中编写ORM框架的入门指引
2015/04/29 Python
Tensorflow实现卷积神经网络用于人脸关键点识别
2018/03/05 Python
python 默认参数相关知识详解
2019/09/18 Python
pytorch nn.Conv2d()中的padding以及输出大小方式
2020/01/10 Python
python实现超级玛丽游戏
2020/03/18 Python
PyQT5速成教程之Qt Designer介绍与入门
2020/11/02 Python
CPB肌肤之钥美国官网:Clé de Peau Beauté
2017/09/05 全球购物
法国床上用品商店:La Compagnie du lit
2019/12/26 全球购物
学雷锋宣传标语
2014/06/25 职场文书
党在我心中的演讲稿
2014/09/13 职场文书
秋季运动会广播稿(30篇)
2014/09/13 职场文书
死亡证明书样本说明
2014/10/18 职场文书
2015个人简历自我评价语
2015/03/11 职场文书
Mysql中 unique列插入重复值该怎么解决呢
2021/05/26 MySQL