在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方法与此相反。不会被此方法编码的字符: @ * / +
英文解释: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.
分享到:
相关推荐
### AJAX传递中文参数乱码解决办法 在Web开发过程中,数据传输是不可或缺的一部分,而AJAX作为一种无需重新加载整个页面的情况下就能与服务器交换数据的技术,被广泛应用于动态数据交互场景中。然而,在处理中文等...
本文将深入探讨如何解决JavaScript(简称JS)在传递中文字符时出现的乱码问题,并提供具体的解决方案。 #### 一、问题背景 在Web应用中,前端页面与后端服务之间通过HTTP请求进行数据交互是非常常见的操作。当这些...
### extjs前后台交互参数出现中文乱码问题的解决方法 #### 问题背景与原因分析 在使用MyEclipse开发工具进行Web应用开发时,尤其是采用ExtJS框架结合Ajax技术进行前后端数据交互的过程中,可能会遇到一个常见的...
当我们需要在两个页面间通过URL传递参数时,经常会遇到中文乱码问题。这个问题主要是因为URL中的参数可能没有经过正确的编码和解码处理。为了解决这个问题,我们可以采取一些方法,特别是当使用jQuery来获取URL参数...
### AJAX技术使用XMLHttpRequest对象传递参数的中文乱码问题 #### 背景与问题概述 在使用AJAX技术进行前后端数据交互时,经常会出现中文字符编码的问题,尤其是在使用`XMLHttpRequest`对象发送请求的过程中。例如...
在使用报表开发工具FineReport设计web报表时,经常需要嵌入iframe到页面中,并给iframe的src属性指定报表地址的同时传递参数值。在参数值、参数名称以及报表名称中,如果包含中文字符或特殊字符,直接传递可能会导致...
在IT行业中,尤其是在进行数据传输、文件存储或者编程过程中,我们常常会遇到“传递汉字丢失或乱码”的问题。这通常是由编码不一致、字符集转换错误或文件读写操作不当等因素引起的。以下是对这个问题的详细分析和...
然而,当我们在GET请求的URL中直接传递包含中文字符的参数时,...总之,处理GET方法中中文参数乱码问题的关键在于正确编码和解码URL中的非ASCII字符。同时,为了安全性与效率,应尽量避免在URL中传递复杂或敏感的信息。
总的来说,解决`jQuery Ajax`传递中文参数乱码问题的方法包括以下步骤: 1. 确保前端页面的`meta`标签指定正确的字符集,如`<meta charset="UTF-8">`。 2. 在`$.ajax`调用中,设置`contentType`为`"application/x-...
3. **JavaScript传递参数**: 当从HTML页面直接跳转到ASP.NET页面并传递中文参数时,可以使用JavaScript的`escape()`函数编码,然后在ASP.NET页面解码。例如: - HTML页面: ```html <script language="...
在实际解决乱码问题的过程中,如果发现使用Base64加密传输中文数据还会出现问题,可以改用URL编码的方式来传输数据,即使用JavaScript的`encodeURI`函数对数据进行两次URL编码,后端接收到后进行一次URL解码即可。...
在Web开发中,经常会遇到通过URL传递参数的情况。其中,`Request.QueryString`是ASP.NET中用于获取URL查询字符串(即URL中问号后的部分)的一种常用方法。然而,在处理包含中文字符的数据时,可能会遇到乱码的问题。...
为了解决奇数个中文字符URL传递乱码的问题,可以采用JavaScript来实现正确的编码处理。具体步骤如下: 1. **获取原始URL**:首先获取用户输入或系统生成的原始URL。 2. **编码处理**: - 使用`encodeURI()`函数...
在Web开发中,使用URL传递参数是一种常见的方式,但在处理中文字符时经常会出现乱码问题。由于HTTP协议本身是基于ASCII字符集,而中文字符不是ASCII字符,所以在URL中的中文字符如果不经过特殊处理就会造成乱码。...
在开发Web应用时,经常会遇到跨语言环境的数据传输问题,特别是在JavaScript(JS)与服务器端Action之间传递包含中文字符的参数时,乱码问题尤为常见。这个问题主要源于字符编码的不同处理,导致数据在传输过程中...
在二维码(QR Code)编码和解码过程中,中文字符可能会出现乱码问题,这通常是由于编码格式不正确或处理方式不当导致的。本教程将详细解释这个问题,并提供一个开源示例帮助你理解和解决中文乱码问题。 首先,我们...
在描述中提到的“中文乱码问题”是ZXing在处理包含中文字符的数据时可能出现的一个常见问题。在默认情况下,ZXing可能无法正确地编码或解码含有非ASCII字符的条码,特别是中文字符,这会导致显示为乱码。解决这个...
在进行Web开发时,尤其是在使用Node.js结合WebStorm IDE环境下,可能会遇到中文字符显示为乱码的问题。这一问题通常发生在网页渲染过程中,特别是在使用模板引擎(如Jade)进行页面渲染时。 #### 二、问题分析 在...
当我们在Web开发中遇到URL地址栏传递中文参数出现乱码的问题时,一个常见的解决办法是使用JavaScript中的escape函数进行转码。这个函数能够将字符串中的中文字符转换为一个十六进制形式的URL编码字符串。在某些...