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 Date自定义函数 延迟脚本执行
Mar 10 Javascript
jQuery学习笔记之jQuery的DOM操作
Dec 22 Javascript
整理一些JavaScript的IE和火狐的兼容性注意事项
Mar 17 Javascript
JavaScript实现当网页加载完成后执行指定函数的方法
Mar 21 Javascript
JavaScript数据类型学习笔记
Jan 25 Javascript
基于javascript实现泡泡大冒险网页版小游戏
Mar 23 Javascript
JS中位置与大小的获取方法
Nov 22 Javascript
谈谈Vue.js——vue-resource全攻略
Jan 16 Javascript
Vue过滤器的用法和自定义过滤器使用
Feb 08 Javascript
jQuery UI Grid 模态框中的表格实例代码
Apr 01 jQuery
ui-router中使用ocLazyLoad和resolve的具体方法
Oct 18 Javascript
vue2.0 + ele的循环表单及验证字段方法
Sep 18 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使用内置dir类实现目录遍历删除
2015/03/31 PHP
8个PHP数组面试题
2015/06/23 PHP
PHP简单生成缩略图相册的方法
2015/07/29 PHP
php生成图片缩略图功能示例
2017/02/22 PHP
php数值转换时间及时间转换数值用法示例
2017/05/18 PHP
javascript里的条件判断
2007/02/27 Javascript
教你如何解密js/vbs/vbscript加密的编码异处理小结
2008/06/25 Javascript
ASP.NET中基于JQUERY的高性能的TreeView补充
2011/02/23 Javascript
jQuery动态设置form表单的enctype值(实现代码)
2013/07/04 Javascript
在javaScript中关于submit和button的区别介绍
2013/10/20 Javascript
jquery xMarquee实现文字水平无缝滚动效果
2014/04/29 Javascript
JS模拟Dialog弹出浮动框效果代码
2015/10/16 Javascript
jquery实现邮箱自动填充提示功能
2015/11/17 Javascript
基于Bootstrap实现下拉菜单项和表单导航条(两个菜单项,一个下拉菜单和登录表单导航条)
2016/07/22 Javascript
Bootstrap table使用方法详细介绍
2016/12/09 Javascript
手把手教你把nodejs部署到linux上跑出hello world
2017/06/19 NodeJs
JQuery 获取Dom元素的实例讲解
2017/07/08 jQuery
微信小程序wx.previewImage预览图片实例详解
2017/12/07 Javascript
react的滑动图片验证码组件的示例代码
2019/02/27 Javascript
Angular4.0动画操作实例详解
2019/05/10 Javascript
在Koa.js中实现文件上传的接口功能
2019/10/08 Javascript
Python 字符串中的字符倒转
2008/09/06 Python
python利用beautifulSoup实现爬虫
2014/09/29 Python
Scrapy-Redis结合POST请求获取数据的方法示例
2019/05/07 Python
详解python中的time和datetime的常用方法
2019/07/08 Python
CSS3媒体查询Media Queries基础学习教程
2016/02/29 HTML / CSS
canvas实现圆绘制的示例代码
2019/09/11 HTML / CSS
北京麒麟网信息技术有限公司网络游戏测试面试题
2013/09/28 面试题
几个常见的消息中间件(MOM)
2014/01/08 面试题
爱岗敬业演讲稿
2014/05/05 职场文书
2014国庆节幼儿园亲子活动方案
2014/09/16 职场文书
父亲去世追悼词
2015/06/23 职场文书
CSS实现漂亮的时钟动画效果的实例代码
2021/03/30 HTML / CSS
PyQt5实现多张图片显示并滚动
2021/06/11 Python
MySQL系列之十 MySQL事务隔离实现并发控制
2021/07/02 MySQL
Go语言实现Base64、Base58编码与解码
2021/07/26 Golang