JS URL傳中文參數引起的亂碼問題

今天的項目中碰到了一個亂碼問題,從JS裏傳URL到服務器,URL中有中文參數,服務器裏讀出的中文參數來的全是「?」,查了網上JS編碼相關資料得以解決。 javascript

解決方法以下:
一、在JS裏對中文參數進行兩次轉碼
html

代碼以下:

var login_name = document.getElementById("loginname").value;
login_name = encodeURI(login_name);
login_name = encodeURI(login_name);





二、在服務器端對參數進行解碼
 代碼以下:

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.
相關文章
相關標籤/搜索