亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

JS URL傳中文參數(shù)引發(fā)的亂碼問題

 更新時(shí)間:2009年09月02日 20:43:15   投稿:mdxy-dxy  
今天的項(xiàng)目中碰到了一個(gè)亂碼問題,從JS里傳URL到服務(wù)器,URL中有中文參數(shù),服務(wù)器里讀出的中文參數(shù)來的全是“?”,查了網(wǎng)上JS編碼相關(guān)資料得以解決。

解決方法如下:

1、在JS里對(duì)中文參數(shù)進(jìn)行兩次轉(zhuǎn)碼

復(fù)制代碼 代碼如下:

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

2、在服務(wù)器端對(duì)參數(shù)進(jìn)行解碼
復(fù)制代碼 代碼如下:

String loginName = ParamUtil.getString(request, "login_name");
loginName = java.net.URLDecoder.decode(loginName,"UTF-8");


在使用url進(jìn)行參數(shù)傳遞時(shí),經(jīng)常會(huì)傳遞一些中文名的參數(shù)或URL地址,在后臺(tái)處理時(shí)會(huì)發(fā)生轉(zhuǎn)換錯(cuò)誤。在有些傳遞頁面使用GB2312,而在接收頁面使用UTF8,這樣接收到的參數(shù)就可能會(huì)與原來發(fā)生不一致。使用服務(wù)器端的urlEncode函數(shù)編碼的URL,與使用客戶端javascript的encodeURI函數(shù)編碼的URL,結(jié)果就不一樣。

javaScript中的編碼方法:

escape() 方法:
采用ISO Latin字符集對(duì)指定的字符串進(jìn)行編碼。所有的空格符、標(biāo)點(diǎn)符號(hào)、特殊字符以及其他非ASCII字符都將被轉(zhuǎn)化成%xx格式的字符編碼(xx等于該字符在字符集表里面的編碼的16進(jìn)制數(shù)字)。比如,空格符對(duì)應(yīng)的編碼是%20。unescape方法與此相反。不會(huì)被此方法編碼的字符: @ * / +

如果是gb2312編碼的可以使用escape,不能用encodeURIComponent,要不會(huì)亂碼。

escape的使用方法:http://chabaoo.cn/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編碼格式轉(zhuǎn)化成escape格式的字符串。不會(huì)被此方法編碼的字符:! @ # $& * ( ) = : / ; ? + '

英文解釋: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編碼格式轉(zhuǎn)化成escape格式的字符串。與encodeURI()相比,這個(gè)方法將對(duì)更多的字符進(jìn)行編碼,比如 / 等字符。所以如果字符串里面包含了URI的幾個(gè)部分的話,不能用這個(gè)方法來進(jìn)行編碼,否則 / 字符被編碼之后URL將顯示錯(cuò)誤。不會(huì)被此方法編碼的字符:! * ( )

英文解釋: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.



因此,對(duì)于中文字符串來說,如果不希望把字符串編碼格式轉(zhuǎn)化成UTF-8格式的(比如原頁面和目標(biāo)頁面的charset是一致的時(shí)候),只需要使用escape。如果你的頁面是GB2312或者其他的編碼,而接受參數(shù)的頁面是UTF-8編碼的,就要采用encodeURI或者encodeURIComponent。

另外,encodeURI/encodeURIComponent是在javascript1.5之后引進(jìn)的,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.

相關(guān)文章

  • JavaScript實(shí)現(xiàn)樹結(jié)構(gòu)轉(zhuǎn)換的五種方法總結(jié)

    JavaScript實(shí)現(xiàn)樹結(jié)構(gòu)轉(zhuǎn)換的五種方法總結(jié)

    在?JavaScript?編程中,將數(shù)組轉(zhuǎn)換為樹結(jié)構(gòu)是一個(gè)常見的需求。本篇博客將介紹五種常用的方法來實(shí)現(xiàn)數(shù)組轉(zhuǎn)樹結(jié)構(gòu),希望對(duì)大家有所幫助
    2023-03-03
  • JavaScript 函數(shù)的定義-調(diào)用、注意事項(xiàng)

    JavaScript 函數(shù)的定義-調(diào)用、注意事項(xiàng)

    這篇文章主要介紹了JavaScript 函數(shù)的定義-調(diào)用、注意事項(xiàng),需要的朋友可以參考下
    2017-04-04
  • TypeScript 映射類型詳情

    TypeScript 映射類型詳情

    這篇文章主要介紹了TypeScript 映射類型詳情,一個(gè)類型需要基于另外一個(gè)類型, 又不想拷貝一份,這個(gè)時(shí)候可以考慮使用映射類型,映射類型建立在索引簽名的語法上,下面文章我們就從回顧下索引簽名展開TypeScript 映射類型的相關(guān)資料,需要的朋友可以參考一下
    2021-12-12
  • 最新評(píng)論