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 相关文章推荐
cloudgamer出品ImageZoom 图片放大效果
Apr 01 Javascript
jQuery1.5.1 animate方法源码阅读
Apr 05 Javascript
jquery选择器-根据多个属性选择示例代码
Oct 21 Javascript
js switch case default 的用法示例介绍
Oct 23 Javascript
详细谈谈AngularJS的子级作用域问题
Sep 05 Javascript
URL的参数中有加号传值变为空格的问题(URL特殊字符)
Nov 04 Javascript
MUI 上拉刷新/下拉加载功能实例代码
Apr 13 Javascript
关于TypeScript中import JSON的正确姿势详解
Jul 25 Javascript
面包屑导航详解
Dec 07 Javascript
微信小程序实现分享到朋友圈功能
Jul 19 Javascript
关于微信小程序map组件z-index的层级问题分析
Jul 09 Javascript
Vue.js仿Select下拉框效果
Feb 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设计模式 Observer(观察者模式)
2011/06/26 PHP
PHP面向对象程序设计OOP继承用法入门示例
2016/12/27 PHP
Yii框架小部件(Widgets)用法实例详解
2020/05/15 PHP
增强用户体验友好性之jquery easyui window 窗口关闭时的提示
2012/06/22 Javascript
jquery在IE、FF浏览器的差别详细探讨
2013/04/28 Javascript
实现只能输入数字的input不用replace方法
2013/09/12 Javascript
JS父页面与子页面相互传值方法
2014/03/05 Javascript
js实现缓冲运动效果的方法
2015/04/10 Javascript
深入解读JavaScript中的Hoisting机制
2015/08/12 Javascript
javascript结合Flexbox简单实现滑动拼图游戏
2016/02/18 Javascript
jQuery自定义数值抽奖活动代码
2016/06/11 Javascript
BootStrap Table复选框默认选中功能的实现代码(从数据库获取到对应的状态进行判断是否为选中状态)
2017/07/11 Javascript
js判断节假日实例代码
2017/12/27 Javascript
微信 jssdk 签名错误invalid signature的解决方法
2019/01/14 Javascript
微信小程序用canvas画图并分享
2020/03/09 Javascript
详解ES6 中的Object.assign()的用法实例代码
2021/01/11 Javascript
python cx_Oracle的基础使用方法(连接和增删改查)
2017/11/19 Python
Python使用MD5加密算法对字符串进行加密操作示例
2018/03/30 Python
详解django的serializer序列化model几种方法
2018/10/16 Python
利用Python将数值型特征进行离散化操作的方法
2018/11/06 Python
python实现对指定字符串补足固定长度倍数截断输出的方法
2018/11/15 Python
python读取csv和txt数据转换成向量的实例
2019/02/12 Python
Tensorflow加载Vgg预训练模型操作
2020/05/26 Python
Python爬虫爬取博客实现可视化过程解析
2020/06/29 Python
Python Opencv图像处理基本操作代码详解
2020/08/31 Python
HTML5 File接口在web页面上使用文件下载
2017/02/27 HTML / CSS
HTML5资源预加载(Link prefetch)详细介绍(给你的网页加速)
2014/05/07 HTML / CSS
HTML5本地数据库基础操作详解
2016/04/26 HTML / CSS
HTML实现代码雨源码及效果示例
2020/02/25 HTML / CSS
优秀党支部书记事迹材料
2014/05/29 职场文书
市场策划求职信
2014/08/07 职场文书
党员干部形式主义个人整改措施
2014/09/17 职场文书
2014物价局群众路线对照检查材料思想汇报
2014/09/21 职场文书
小学三年级数学教学反思
2016/02/16 职场文书
SpringBoot工程下使用OpenFeign的坑及解决
2021/07/02 Java/Android
Redis调用Lua脚本及使用场景快速掌握
2022/03/16 Redis