JSURL传中⽂参数引发的乱码问题
解决⽅法如下:
1、在JS⾥对中⽂参数进⾏两次转码
复制代码代码如下:
var login_name = ElementById("loginname").value;
login_name = encodeURI(login_name);
login_name = encodeURI(login_name);
2、在服务器端对参数进⾏解码
复制代码代码如下:
String loginName = String(request, "login_name");
loginName = java.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的使⽤⽅法:www.jb51/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 o
ther 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格式的字符串。与enc
url编码和utf8区别odeURI()相⽐,这个⽅法将对更多的字符进⾏编码,⽐如 / 等字符。所以如果字符串⾥⾯包含了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.
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论