今天的项目中碰到了一个乱码问题,从JS里传URL到服务器,URL中有中文参数,服务器里读出的中文参数来的全是“?”,查了网上JS编码相关资料得以解决。
解决方法如下:
1、在JS里对中文参数进行两次转码
var login_name = document.getElementById("loginname").value;
login_name = encodeURI(login_name);
// login_name = encodeURI(login_name); 一般只需要encodeURI一次,亲测encodeURI两次的时候在IE中出现乱码,firefox中正常...encodeURI次数需根据实际开发情况来定了...
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方法与此相反。不会被此方法编码的字符: @ * / +
英文解释: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.
1、在JS里对中文参数进行两次转码
复制代码代码如下:
var login_name = document.getElementById("loginname").value;
login_name = encodeURI(login_name);
// login_name = encodeURI(login_name); 一般只需要encodeURI一次,亲测encodeURI两次的时候在IE中出现乱码,firefox中正常...encodeURI次数需根据实际开发情况来定了...
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方法与此相反。不会被此方法编码的字符: @ * / +
英文解释: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.
相关推荐
然而,不同的浏览器可能会使用不同的默认编码,比如GBK,这可能导致编码不一致,从而引发乱码问题。 当服务器(例如Tomcat)接收到GET请求时,它会默认使用ISO-8859-1字符集来解码URL中的参数。由于ISO-8859-1并不...
在IT行业中,尤其是在Java编程领域,中文乱码问题是一个常见的挑战。这主要涉及到字符编码的处理,涉及到Unicode、GBK、UTF-8等不同编码格式之间的转换和一致性问题。本篇文章将深入探讨这个问题,并提供一种彻底...
1. **确保正确编码**:在前端,使用`encodeURIComponent()`函数对URL参数进行编码,确保所有特殊字符都被正确处理。例如: ```javascript let urlValue = encodeURIComponent("包含空格和特殊字符的字符串"); ```...
在JavaScript中使用`encodeURIComponent()`函数对参数进行编码,该函数可以将特殊字符转换为适用于URL的标准格式,从而避免传输过程中的编码问题。例如: ```javascript document.all.WebOffice1.HttpAddPostString...
在JavaServer Pages (JSP) 开发中,页面间的参数传递是常见的操作,尤其是在处理包含汉字的参数时,由于编码问题可能会导致乱码。本文主要介绍如何在JSP中正确地进行汉字参数的转码与解码,确保数据在页面间传递时...
这个问题主要源于请求参数在URL中编码时使用的是UTF-8以外的字符集,导致服务器接收到的参数解码后出现乱码。本文将深入探讨这个问题,并提供解决方案。 一、问题解析 1. GET请求原理:GET请求的参数通常会附加在...
在上述情况中,开发者尝试解决由URL编码引发的乱码问题,主要涉及到两个方法: 方法1: 这种方法试图通过在后台将ISO-8859-1编码的字符串转换为UTF-8编码。首先,从HTTP请求中获取参数(如`work`),然后使用`...
页面间值传递有多种方式,包括URL参数、Cookie、LocalStorage、SessionStorage、全局变量、事件触发等。以下是一些常用方法: 1. URL参数:适用于简单的值传递,通过修改或解析URL的查询字符串来实现。 ```...
这是因为IE6将URL编码为UTF-8,而Tomcat可能仍按照默认的ISO-8859-1解码,从而引发乱码。 请求响应流程如下: 1. 表单GET提交:浏览器根据页面charset编码请求,Tomcat根据URIEncoding解码。 2. 表单POST提交:过滤...
此外,中文乱码问题在GET请求中较为常见,因为URL编码可能导致字符显示不正确。 2. POST提交:POST方法是用于向服务器发送数据的主要方法,适合传输大量数据和敏感信息。在AJAX中,POST数据包含在请求体中,不受URL...
- **JS URL传中文参数引发的乱码问题**:探讨了在处理含有中文字符的URL参数时可能出现的问题及解决方案。 通过以上介绍,我们可以看到`request`函数在处理URL参数方面具有很大的灵活性和实用性。无论是基本的参数...
5. 字符流操作不当:在处理输入输出流时,如果没有指定正确的字符编码,例如读取或写入文件,也可能引发乱码。 针对这些问题,我们可以采取以下措施进行解决: 1. 使用统一的编码:确保整个项目的文件都使用UTF-8...
9. **URL解码问题**:解码时要注意URL编码可能会引发的XSS(跨站脚本攻击)或SQL注入等安全问题,因此在处理用户输入时应谨慎。 10. **编码与解码的最佳实践**:在处理URL时,应该始终使用适当的编码和解码函数,而...
在使用JQuery的`ajaxSubmit`方法提交包含中文字符的数据时,可能会遇到乱码问题,尤其是在不同的浏览器中表现不一致。这是因为浏览器在处理字符编码时的差异,以及服务器端解码方式的不同。以下是对这个问题的深入...
需要注意的是,由于同源策略(Same-origin policy)的存在,如果`iframe`加载的内容来自于不同的域名,那么在主页面中通过JavaScript访问`iframe`内部文档的属性会受到限制,可能会引发安全错误。解决这一问题的方法...
当你的JavaScript文件和HTML页面的字符编码不一致时,可能会导致字符串中的特殊字符因编码问题而显示为乱码,进而引发“未结束的字符串常量”的错误。例如,如果HTML声明为`charset="UTF-8"`,但JavaScript文件实际...
7. 解决 JSP 中文乱码问题:可以通过在页面头部设置 charset,利用过滤器对请求进行转码,或者对 `request` 和 `response` 进行编码设置来解决中文乱码问题。系统自动设置一般不可靠,需要开发者手动处理。 8. ...