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 相关文章推荐
EasyUI中datagrid在ie下reload失败解决方案
Mar 09 Javascript
JQuery boxy插件在IE中边角图片不显示问题的解决
May 20 Javascript
JQuery EasyUI的使用
Feb 24 Javascript
获取当前月(季度/年)的最后一天(set相关操作及应用)
Dec 27 Javascript
微信小程序 数组中的push与concat的区别
Jan 05 Javascript
JS检测是否可以访问公网服务器功能代码
Jun 19 Javascript
微信小程序购物车、父子组件传值及calc的注意事项总结
Nov 14 Javascript
详解关于表格合并span-method方法的补充(表格数据由后台动态返回)
May 21 Javascript
javascript 易错知识点实例小结
Apr 25 Javascript
vue下载二进制流图片操作
Oct 26 Javascript
JavaScript实现简单动态表格
Dec 02 Javascript
vue使用过滤器格式化日期
Jan 20 Vue.js
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
分享10段PHP常用代码
2015/11/11 PHP
php 截取utf-8格式的字符串实例代码
2016/10/30 PHP
让whoops帮我们告别ThinkPHP6的异常页面
2020/03/02 PHP
TNC vs BOOM BO3 第一场2.13
2021/03/10 DOTA
父窗口获取弹出子窗口文本框的值
2006/06/27 Javascript
jquery实现excel导出的方法
2013/04/04 Javascript
jQuery动态添加、删除元素的方法
2014/01/09 Javascript
如何判断微信内置浏览器(通过User Agent实现)
2014/09/01 Javascript
jQuery实现的导航动画效果(附demo源码)
2016/04/01 Javascript
javascript的函数劫持浅析
2016/09/26 Javascript
JavaScript使用链式方法封装jQuery中CSS()方法示例
2017/04/07 jQuery
JavaScript表单即时验证 验证不成功不能提交
2017/08/31 Javascript
使用JavaScript实现在页面中显示距离2017年中秋节的天数
2017/09/26 Javascript
angular2中使用第三方js库的实例
2018/02/26 Javascript
详解微信小程序用定时器实现倒计时效果
2019/04/30 Javascript
Vue 实现输入框新增搜索历史记录功能
2019/10/15 Javascript
Vue最新防抖方案(必看篇)
2019/10/30 Javascript
Webpack中SplitChunksPlugin 配置参数详解
2020/03/24 Javascript
JavaScript进阶(一)变量声明提升实例分析
2020/05/09 Javascript
解决vue中的无限循环问题
2020/07/27 Javascript
[51:05]DOTA2上海特级锦标赛主赛事日 - 5 败者组决赛Liquid VS EG第一局
2016/03/06 DOTA
python实现的登录和操作开心网脚本分享
2014/07/09 Python
Python类的动态修改的实例方法
2017/03/24 Python
老生常谈python之鸭子类和多态
2017/06/13 Python
Python实现将sqlite数据库导出转成Excel(xls)表的方法
2017/07/17 Python
python读取几个G的csv文件方法
2019/01/07 Python
matlab、python中矩阵的互相导入导出方式
2020/06/01 Python
诗普兰迪官方网站:Splendid
2018/09/18 全球购物
大四自我鉴定范文
2013/10/06 职场文书
学生打架检讨书大全
2014/01/23 职场文书
教师师德师风自我剖析材料
2014/09/29 职场文书
领导欢送会主持词
2015/07/06 职场文书
公司劳动纪律管理制度
2015/08/04 职场文书
2015年大学组织委员个人工作总结
2015/10/23 职场文书
演讲稿之感恩老师(三篇范文)
2019/09/06 职场文书
vue2实现provide inject传递响应式
2021/05/21 Vue.js