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 篱式条件判断
Aug 22 Javascript
JavaScript日历实现代码
Sep 12 Javascript
javascript定时保存表单数据的代码
Mar 17 Javascript
Js中使用hasOwnProperty方法检索ajax响应对象的例子
Dec 08 Javascript
javascript每日必学之循环
Feb 19 Javascript
JavaScript 轮播图和自定义滚动条配合鼠标滚轮分享代码贴
Oct 28 Javascript
Jquery Easyui菜单组件Menu使用详解(15)
Dec 18 Javascript
详解Vue使用 vue-cli 搭建项目
Apr 20 Javascript
JavaScript实现简单动态进度条效果
Apr 06 Javascript
jQuery创建及操作xml格式数据示例
May 26 jQuery
对angularjs框架下controller间的传值方法详解
Oct 08 Javascript
微信小程序去除左上角返回键的实现方法
Mar 06 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的引用计数机制
2013/06/14 PHP
解析PHP高效率写法(详解原因)
2013/06/20 PHP
php+xml结合Ajax实现点赞功能完整实例
2015/01/30 PHP
yii2使用GridView实现数据全选及批量删除按钮示例
2017/03/01 PHP
laravel实现批量更新多条记录的方法示例
2017/10/22 PHP
Javascript的IE和Firefox兼容性汇编
2006/07/01 Javascript
jquery创建并行对象或者合并对象的实现代码
2012/10/10 Javascript
jquery选择符快速提取web表单数据示例
2014/03/27 Javascript
JavaScript利用append添加元素报错的解决方法
2014/07/01 Javascript
Extjs根据条件设置表格某行背景色示例
2014/07/23 Javascript
修改或扩展jQuery原生方法的代码实例
2015/01/13 Javascript
location.hash保存页面状态的技巧
2016/04/28 Javascript
Bootstrap源码解读网格系统(3)
2016/12/22 Javascript
js移动端事件基础及常用事件库详解
2017/08/15 Javascript
浅谈Angular4中常用管道
2017/09/27 Javascript
vue中遇到的坑之变化检测问题(数组相关)
2017/10/13 Javascript
微信小程序支付功能 php后台对接完整代码分享
2018/06/12 Javascript
基于vue v-for 多层循环嵌套获取行数的方法
2018/09/26 Javascript
Python的SimpleHTTPServer模块用处及使用方法简介
2018/01/22 Python
利用Django-environ如何区分不同环境
2018/08/26 Python
python scrapy重复执行实现代码详解
2019/12/28 Python
利用pytorch实现对CIFAR-10数据集的分类
2020/01/14 Python
Python键鼠操作自动化库PyAutoGUI简介(小结)
2020/05/17 Python
Python 调用C++封装的进一步探索交流
2021/03/04 Python
CSS3制作苹果风格键盘特效
2015/02/26 HTML / CSS
Carolina工作鞋官网:Carolina Footwear
2019/03/14 全球购物
行政经理岗位职责
2013/11/09 职场文书
北京大学自荐信范文
2014/01/28 职场文书
优秀教师工作感言
2014/02/16 职场文书
工作睡觉检讨书
2014/02/25 职场文书
会务接待方案
2014/02/27 职场文书
给校长的建议书300字
2014/05/16 职场文书
音乐幼师求职信
2014/07/09 职场文书
浙江省杭州市平均工资标准是多少?
2019/07/09 职场文书
Java 将PPT幻灯片转为HTML文件的实现思路
2021/06/11 Java/Android
Python Matplotlib绘制条形图的全过程
2021/10/24 Python