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 相关文章推荐
xmlHTTP实例
Oct 24 Javascript
(function(){})()的用法与优点
Mar 11 Javascript
使用jquery获取网页中图片高度的两种方法
Sep 26 Javascript
可插入图片的TEXT文本框
Dec 27 Javascript
jQuery简单实现title提示效果示例
Aug 01 Javascript
JavaScript如何实现跨域请求
Aug 05 Javascript
jQuery 利用$.ajax 时获取原生XMLHttpRequest 对象的方法
Aug 25 Javascript
js实现二级菜单点击显示当前内容效果
Apr 28 Javascript
vue.js 添加 fastclick的支持方法
Aug 28 Javascript
微信小程序MUI侧滑导航菜单示例(Popup弹出式,左侧不动,右侧滑动)
Jan 23 Javascript
javascript设计模式 ? 命令模式原理与用法实例分析
Apr 20 Javascript
解决Ant Design Modal内嵌Form表单initialValue值不动态更新问题
Oct 29 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扩展vld查看PHP opcode操作步骤
2013/03/04 PHP
PHP源码分析之变量的存储过程分解
2014/07/03 PHP
PHPMailer发送邮件
2016/12/28 PHP
基于yaf框架和uploadify插件,做的一个导入excel文件,查看并保存数据的功能
2017/01/24 PHP
ajax调用返回php接口返回json数据的方法(必看篇)
2017/05/05 PHP
浅谈PHP SHA1withRSA加密生成签名及验签
2019/03/18 PHP
PHP goto语句用法实例
2019/08/06 PHP
Laravel实现ORM带条件搜索分页
2019/10/24 PHP
JQuery浮动DIV提示信息并自动隐藏的代码
2010/08/29 Javascript
ie下动态加态js文件的方法
2011/09/13 Javascript
jQuery获得内容和属性示例代码
2014/01/16 Javascript
nodejs 整合kindEditor实现图片上传
2015/02/03 NodeJs
高性能JavaScript模板引擎实现原理详解
2015/02/05 Javascript
JS获取网页图片name属性的方法
2015/04/01 Javascript
angularJS与bootstrap结合实现动态加载弹出提示内容
2015/10/16 Javascript
AngularJS基础 ng-include 指令示例讲解
2016/08/01 Javascript
用Vue.js实现监听属性的变化
2016/11/17 Javascript
js实现下拉框效果(select)
2017/03/28 Javascript
node下使用UglifyJS压缩合并JS文件的方法
2018/03/07 Javascript
开发用到的js封装方法(20种)
2018/10/12 Javascript
浅谈JavaScript_DOM学习篇_图片切换小案例
2019/03/19 Javascript
vue.js 打包时出现空白页和路径错误问题及解决方法
2019/06/26 Javascript
JavaScript 替换所有匹配内容及正则替换方法
2020/02/12 Javascript
vue+element实现图片上传及裁剪功能
2020/06/29 Javascript
详解微信小程序入门从这里出发(登录注册、开发工具、文件及结构介绍)
2020/07/21 Javascript
[10:18]2018DOTA2国际邀请赛寻真——Fnatic能否笑到最后?
2018/08/14 DOTA
python使用pandas处理大数据节省内存技巧(推荐)
2019/05/05 Python
Python如何实现远程方法调用
2020/08/07 Python
红色连衣裙精品店:Red Dress Boutique
2018/08/11 全球购物
斯巴达比赛商店:Spartan Race
2019/01/08 全球购物
三星法国官方网站:Samsung法国
2019/10/31 全球购物
SEPHORA丝芙兰德国官方购物网站:化妆品、护肤品和香水
2020/01/21 全球购物
人力资源作业细则
2014/03/03 职场文书
2014年生产管理工作总结
2014/12/23 职场文书
冰峪沟导游词
2015/02/09 职场文书
Java 多线程并发FutureTask
2022/06/28 Java/Android