解决url中包含中文导致的乱码

转载 2015年11月17日 18:24:27

今天的项目中碰到了一个乱码问题,从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,结果就不一样。 

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

JS中默认编码导致URL重写时中文参数乱码

问题场景:触发GIS绑定的单击事件时,通过window对象提交url,附带“省份”,在页面加载完成时参数出现乱码,但地址栏中的参数正常? 问题解决: 网络版本之一:通过IDE修改外联的JS编码格式 我...

indy httpserver 接收URL包含中文参数乱码的问题

在测试TIdHttpServer的时候,发现浏览器提交的URL包含中文时会乱码,我用的是XE7 UPDATE1,INDY是10。 procedure TForm1.idhtpsrv1CommandG...

url中向后台传递中文乱码解决方法

 方法一: 1、jsp中代码   var userNo = $('#prisoner_id').val();       userNo = encodeURI(userNo);     ...

解决get方法传递URL参数中文乱码问题

应用一:解决tomcat下中文乱码问题(先来个简单的)  在tomcat下,我们通常这样来解决中文乱码问题: 过滤器代码: package filter;      import java....

Java web的URL地址参数传递中文乱码的解决方案

系统很多Url地址都暴露给用户,存在安全隐患,用户可以去随意修改Url地址和参数值,为了解决这个问题提供以下解决方案,具体步骤如下: 第一步:编码URL地址,调用CommonMethod.js的re...

3大URL传参中文乱码解决办法

URL传参中文对于程序员来说,是个很头疼的问题吧。虽然在尽量避免使用中文参数,但总有避免不了的时候。现就自己的经验,总结如下:         1.将字符串转码:new String(“xxxx...

解决get方法传递URL参数中文乱码问题

package filter;      import java.io.*;   import javax.servlet.*;   import javax.servlet.http.*; ...
  • lwwgtm
  • lwwgtm
  • 2014年06月14日 21:31
  • 442

javascript通过url向jsp页面传递中文参数乱码解决方法

2013-1-6 10:35:49 org.apache.tomcat.util.http.Parameters processParameters 警告: Parameters: Characte...

解决get方法传递URL参数中文乱码问题

get post 乱码问题
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:解决url中包含中文导致的乱码
举报原因:
原因补充:

(最多只允许输入30个字)