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 相关文章推荐
js中通过split函数分割字符串成数组小例子
Sep 21 Javascript
深入理解JavaScript高级之词法作用域和作用域链
Dec 10 Javascript
Jquery插件分享之气泡形提示控件grumble.js
May 20 Javascript
jquery实现焦点图片随机切换效果的方法
Mar 12 Javascript
js canvas仿支付宝芝麻信用分仪表盘
Nov 16 Javascript
Express框架之connect-flash详解
May 31 Javascript
Vue实现virtual-dom的原理简析
Jul 10 Javascript
EasyUI的DataGrid绑定Json数据源的示例代码
Dec 16 Javascript
vue-cli 使用axios的操作方法及整合axios的多种方法
Sep 12 Javascript
jQuery实现的点击显示隐藏下拉菜单功能完整示例
May 17 jQuery
vue路由传参三种基本方式详解
Dec 09 Javascript
vue 实现一个简单的全局调用弹窗案例
Sep 10 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
全国FM电台频率大全 - 17 湖北省
2020/03/11 无线电
windows下PHP APACHE MYSQ完整配置
2007/01/02 PHP
PHP数组游标实现对数组的各种操作详解
2016/01/26 PHP
javascript中的注释使用与注意事项小结
2011/09/20 Javascript
js面向对象设计用{}好还是function(){}好(构造函数)
2011/10/23 Javascript
仿谷歌主页js动画效果实现代码
2013/07/14 Javascript
设置jsf的选择框h:selectOneMenu为不可编辑状态的方法
2014/01/07 Javascript
JavaScript函数的4种调用方法详解
2014/04/22 Javascript
java、javascript实现附件下载示例
2014/08/14 Javascript
jQuery处理图片加载失败的常用方法
2015/06/08 Javascript
js实现页面跳转的几种方法小结
2016/05/16 Javascript
@angular前端项目代码优化之构建Api Tree的方法
2018/12/24 Javascript
TypeScript中的方法重载详解
2019/04/12 Javascript
解决vue跨域axios异步通信问题
2019/04/17 Javascript
vue mvvm数据响应实现
2020/11/11 Javascript
[01:03]PWL开团时刻DAY6——别打我
2020/11/05 DOTA
pandas dataframe添加表格框线输出的方法
2019/02/08 Python
浅谈PYTHON 关于文件的操作
2019/03/19 Python
Python使用sklearn实现的各种回归算法示例
2019/07/04 Python
Django中使用session保持用户登陆连接的例子
2019/08/06 Python
linux下python中文乱码解决方案详解
2019/08/28 Python
基于Python爬取搜狐证券股票过程解析
2020/11/18 Python
CSS3实现头像旋转效果
2017/03/13 HTML / CSS
HTML5在a标签内放置块级元素示例代码
2013/08/23 HTML / CSS
会计出纳岗位职责
2013/12/25 职场文书
银行办理业务介绍信
2014/01/18 职场文书
工作违纪检讨书
2014/02/17 职场文书
遗嘱继承公证书
2014/04/09 职场文书
个人诉讼委托书范本
2014/10/17 职场文书
2015年服务员个人工作总结
2015/05/27 职场文书
医院病假条范文
2015/08/17 职场文书
python自动化调用百度api解决验证码
2021/04/13 Python
浅谈python数据类型及其操作
2021/05/25 Python
react antd实现动态增减表单
2021/06/03 Javascript
python 远程执行命令的详细代码
2022/02/15 Python
python和Appium的移动端多设备自动化测试框架
2022/04/26 Python