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实现的模拟jquery的animate自定义动画(2.5K)
Jul 20 Javascript
jquery中邮箱地址 URL网站地址正则验证实例代码
Sep 15 Javascript
js实现点击链接后延迟3秒再跳转的方法
Jun 05 Javascript
简介JavaScript中的getSeconds()方法的使用
Jun 10 Javascript
简介AngularJS的HTML DOM支持情况
Jun 17 Javascript
JavaScript实现将数组数据添加到Select下拉框的方法
Aug 21 Javascript
javascript实现自动填写表单实例简析
Dec 02 Javascript
百度地图给map添加右键菜单(判断是否为marker)
Mar 04 Javascript
基于jQuery和hwSlider实现内容左右滑动切换效果附源码下载(一)
Jun 22 Javascript
jquery animate动画持续运动的实例
Nov 29 jQuery
vue操作下拉选择器获取选择的数据的id方法
Aug 24 Javascript
浅谈Webpack核心模块tapable解析
Sep 11 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和vue配合使用技巧和方法
2019/05/09 PHP
PHP实现简单用户登录界面
2019/10/23 PHP
jquery photoFrame 图片边框美化显示插件
2010/06/28 Javascript
jquery zTree异步加载简单实例分享
2013/02/05 Javascript
用JavaScript实现动画效果的方法
2013/07/20 Javascript
jQuery抛物线运动实现方法(附完整demo源码下载)
2016/01/08 Javascript
JS获取鼠标坐标位置实例分析
2016/01/20 Javascript
ES6中非常实用的新特性介绍
2016/03/10 Javascript
浅谈JavaScript的全局变量与局部变量
2016/06/10 Javascript
基于LayUI分页和LayUI laypage分页的使用示例
2017/08/02 Javascript
使用 Vue 绑定单个或多个 Class 名的实例代码
2018/01/08 Javascript
MVVM 双向绑定的实现代码
2018/06/21 Javascript
bootstrap select2插件用ajax来获取和显示数据的实例
2018/08/09 Javascript
在axios中使用params传参的时候传入数组的方法
2018/09/25 Javascript
vue 使用高德地图vue-amap组件过程解析
2019/09/07 Javascript
微信小程序 flexbox layout快速实现基本布局的解决方案
2020/03/24 Javascript
[48:00]EG vs LGD 2018国际邀请赛淘汰赛BO3 第二场 8.26
2018/08/29 DOTA
Python and、or以及and-or语法总结
2015/04/14 Python
用实例解释Python中的继承和多态的概念
2015/04/27 Python
Python常见工厂函数用法示例
2018/03/21 Python
python实现复制文件到指定目录
2019/10/16 Python
基于Python中isfile函数和isdir函数使用详解
2019/11/29 Python
scrapy redis配置文件setting参数详解
2020/11/18 Python
一款纯css3实现的颜色渐变按钮的代码教程
2014/11/12 HTML / CSS
美国最大的骑马用品零售商:HorseLoverZ
2017/01/12 全球购物
美国在线购买和出售礼品卡网站:EJ Gift Cards
2019/06/09 全球购物
COSETTE官网:奢华,每天
2020/03/22 全球购物
学生会主席演讲稿
2014/04/25 职场文书
小学语文教学经验交流材料
2014/06/02 职场文书
计算机相关专业自荐信
2014/07/02 职场文书
合唱兴趣小组活动总结
2014/07/10 职场文书
2014年公务员退休工资改革方案
2014/10/01 职场文书
党的群众路线整改落实情况汇报
2014/10/28 职场文书
贷款工资证明范本
2015/06/12 职场文书
如何理解Vue简单状态管理之store模式
2021/05/15 Vue.js
Java 通过手写分布式雪花SnowFlake生成ID方法详解
2022/04/07 Java/Android