一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

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

解決方法如下:
1、在JS里對中文參數(shù)進行兩次轉(zhuǎn)碼
復(fù)制代碼 代碼如下:
var login_name = document.getElementById("loginname").value;
login_name = encodeURI(login_name);
login_name = encodeURI(login_name);

2、在服務(wù)器端對參數(shù)進行解碼
復(fù)制代碼 代碼如下:
String loginName = ParamUtil.getString(request, "login_name");
loginName = Java.NET.URLDecoder.decode(loginName,"UTF-8");

在使用url進行參數(shù)傳遞時,經(jīng)常會傳遞一些中文名的參數(shù)或URL地址,在后臺處理時會發(fā)生轉(zhuǎn)換錯誤。在有些傳遞頁面使用GB2312,而在接收頁面使用UTF8,這樣接收到的參數(shù)就可能會與原來發(fā)生不一致。使用服務(wù)器端的urlEncode函數(shù)編碼的URL,與使用客戶端Javascript的encodeURI函數(shù)編碼的URL,結(jié)果就不一樣。
JavaScript中的編碼方法:
escape() 方法:
采用ISO Latin字符集對指定的字符串進行編碼。所有的空格符、標點符號、特殊字符以及其他非ASCII字符都將被轉(zhuǎn)化成%xx格式的字符編碼(xx等于該字符在字符集表里面的編碼的16進制數(shù)字)。比如,空格符對應(yīng)的編碼是%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編碼格式轉(zhuǎn)化成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編碼格式轉(zhuǎn)化成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.



因此,對于中文字符串來說,如果不希望把字符串編碼格式轉(zhuǎn)化成UTF-8格式的(比如原頁面和目標頁面的charset是一致的時候),只需要使用escape。如果你的頁面是GB2312或者其他的編碼,而接受參數(shù)的頁面是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.

JavaScript技術(shù)JS URL傳中文參數(shù)引發(fā)的亂碼問題,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 久久夜色国产精品噜噜 | 色忧忧| 激情六月婷婷开心丁香开心 | 国产成人精品一区二区不卡 | 国产综合在线视频 | 亚洲成人国产精品 | 男啪女色黄无遮挡免费视频 | 婷婷综合在线 | 久久久青草青青国产亚洲免观 | 国产精品一区二区在线观看 | 亚洲综合图片人成综合网 | 日本欧美久久久久免费播放网 | www.亚洲视频| 亚洲国产精品日韩高清秒播 | 午夜毛片网站 | 国产精自产拍久久久久久 | 中文字幕亚洲激情 | 久久久噜噜噜久久网 | 日本一区二区三区在线 视频 | 福利视频专区 | 91久久婷婷国产综合精品青草 | 91高清视频在线观看 | 国产成人久久精品激情91 | 欧美成人午夜视频 | 日本免费一区二区三区a区 日本免费一区二区三区看片 | 国产福利在线小视频 | 欧美激情文学 | 欧美激情视频二区 | 91视频看 | 美女毛片在线看 | 国产91精品在线观看 | 亚洲激情婷婷 | 精品日韩欧美国产一区二区 | 国产成人在线播放视频 | 久99久女女精品免费观看69堂 | 在线色综合| 男女国产视频 | 亚洲美女福利视频 | 色涩播| 国产老鸭窝毛片一区二区 | 国产精品 主播精选 网红 |