JS URL传中文参数引发的乱码问题

今天的项目中碰到了一个乱码问题,从JS里传URL到服务器,URL中有中文参数,服务器里读出的中文参数来的全是“?”,查了网上JS编码相关资料得以解决。

解决方法一:

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,结果就不一样。

解决方法二:

前台:

login_name = encodeURIComponent(login_name);//编码一次

action中:

String temp = new String(keyword.getBytes("ISO-8859-1"),"utf-8");
keyword = URLDecoder.decode(temp, "utf-8"); //keyword 是参数名

javaScript中的编码方法:

escape() 方法:

采用ISO Latin字符集对指定的字符串进行编码。所有的空格符、标点符号、特殊字符以及其他非ASCII字符都将被转化成%xx格式的字符编码(xx等于该字符在字符集表里面的编码的16进制数字)。比如,空格符对应的编码是%20。unescape方法与此相反。不会被此方法编码的字符: @ * / +

如果是gb2312编码的可以使用escape,不能用encodeURIComponent,要不会乱码。

escape的使用方法:http://www.jb51.net/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.

本文转自:http://www.jb51.net/article/19850.htm

http://blog.csdn.net/elfenliedef/article/details/6115152

时间: 2024-08-25 16:40:51

JS URL传中文参数引发的乱码问题的相关文章

JS URL传递中文参数时出现乱码的处理

原文:JS URL传递中文参数时出现乱码的处理 在浏览器中显示的地址是这样的: 但是按F12调试的时候的地址却变化掉了: 这个肯定是是因为浏览器对url路径默认编码了.这个问题是在我们去取值的时候,得到的就是后面那一大串稀奇古怪的东西.得不到我们想要的值.怎么办呢?这里写出来的只是自己的解决方法,也许不能通用,跟大家分享下,有更好的方法,告我一声.      //得到url中的值 function request(paras){ var url = location.href; var para

中文参数引发的乱码问题

新手一枚,使用spring MVC框架开发项目 路径中文传输变乱码 找到的解决方法 JS URL传中文参数引发的乱码问题 解决方法如下: 1.在JS里对中文参数进行两次转码  代码如下: var login_name = document.getElementById("loginname").value; login_name = encodeURI(login_name); login_name = encodeURI(login_name);//必须加密两次 或 window.l

URL传中文参数导致乱码的解决方案之encodeURI

通过URL传中文参数时,在服务端后台获取到的值往往会出现乱码问题,解决方案有很多种,本文主要介绍如何通过encodeURI来解决中文乱码问题: first:前端传递参数的时候需要对中文参数进行两次encodeURI处理: ?var requestUrl = 'url?roleName='+encodeURI(encodeURI("rowObj.appName")); 注:rowObj.appName?表示即将传到后台的带中文的字符串 ?second:在服务器端后台程序代码中要用java

url传中文参数的时候,乱码,需要对web.config配置

1,form表单提交到某个具体页面 <form id="search_form" name="search_form" target="_blank" method="Post"> <input id="catid" name="catid" type="hidden" value="0"/> <input class

HTML的Get方法URL传递中文参数,解决乱码问题

本例中有使用JQuery. 资料参考:http://www.cnblogs.com/babycool/p/3169058.html 发送的HTML页面代码: 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Index</title> 6 <script type="text/javascript"

从js向Action传中文参数出现乱码问题的解决方法

Action获取jsp表单中的中文参数,只要整个项目都采用UTF-8编码格式都不会出现乱码问题:但JSP中用到JS,并从JS向Action传中文参数,就会出现中文乱的现象. 经过实践发现下面的方法可以解决中文乱码问题: JSP的JS中:中文参数用encodeURI(encodeURI(中文参数)),经过两次转码.例如: function show(next,id,realName){ document.forms['f2'].action="usersearchNextPage?next=&qu

jsp的url后跟中文参数传参出现乱码

①重新编码:String urlParam= request.getParameter("urlParam");  urlParam= new String(urlParam.getBytes("ISO-8859-1"), "UTF-8"); ②tomcat中统一编码 tomcat  的server.xml中在相对应的端口中加下面两句useBodyEncodingForURI="true" URIEncoding="

url 传递中文参数乱码问题的终极解决方法。

估计很多人在做web开发的时候,都会碰到过url传递中文参数,有时候会出现乱码的问题,但有些项目或者环境,又不会有问题.当遇到乱码的时候,上网找了很多解决方案,比如: 页面设置它的编码方式,改成utf-8 或者gb2312. encodeURI(url),也有人说要2个encodeURI,如:window.location.href = encodeURI('b.html?cId='+id+"&cName="+encodeURIComponent(name)); 然后后台Str

js url传值中文乱码之解决之道

因为js url在传值的过程中使用的是js自己默认的字符集编码规则,我们必须把它转成属于我们自己的编码规格 在websphere 中使用的是url=encodeURI(encodeURI(url)); //用了2次encodeURI 测试成功,第一次转换没有尝试, 处理方法一. js 程序代码:url=encodeURI(url);注意是整个URL 服务器端的代码:String linename = new String(request.getParameter("name").get