国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

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

2024-05-06 14:14:11
字體:
來源:轉載
供稿:網友

解決方法如下:

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的使用方法://m.survivalescaperooms.com/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將顯示錯誤。不會被此方法編碼的字符:! * ( )

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 景德镇市| 德州市| 临海市| 屯昌县| 修水县| 冕宁县| 汝阳县| 澄江县| 泊头市| 贵溪市| 建始县| 无棣县| 高密市| 天峨县| 泗洪县| 宁都县| 新乐市| 水富县| 太白县| 高邑县| 黎川县| 临洮县| 威宁| 兴业县| 哈密市| 湖北省| 唐山市| 屯留县| 元朗区| 景宁| 原平市| 郓城县| 利辛县| 平利县| 西安市| 焦作市| 芷江| 科技| 定边县| 荃湾区| 永登县|