天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

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

解決方法如下:
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方法與此相反。不會被此方法編碼的字符: @ * / +

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

JavaScript技術JS URL傳中文參數引發的亂碼問題,轉載需保留來源!

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

主站蜘蛛池模板: 美女脱得只剩皮肤 | 久久久久久91香蕉国产 | 成人国产精品玖玖热色欲 | Y8848高清私人影院软件优势 | 免费看美女的网站 | BL文高H强交 | 午夜性爽视频男人的天堂在线 | 欧美日韩国产在线一区二区 | 天美传媒在线观看免费完整版 | 丰满大屁俄罗斯肥女 | 国产成人久久精品AV | 热热久久超碰精品中文字幕 | 亚洲精品一卡二卡三卡四卡2021 | www免费看.男人的天堂 | 亚洲精品123区 | 1000视频在线播放 | 国产学生在线播放精品视频 | 果冻传媒完整免费网站在线观看 | 亚洲欧美综合乱码精品成人网 | 人妻熟女斩五十路0930 | 午夜性伦鲁啊鲁免费视频 | 日本久久久久亚洲中字幕 | 女人爽到高潮嗷嗷叫视频 | 日本艳妓BBW高潮一19 | 欧美.亚洲.日韩.天堂 | 港台三级大全 | 办公室里做好紧好爽H | 精品国产麻豆免费人成网站 | 高龄熟女50P | 亚洲在线成色综合网站 | 动漫美女无衣 | 亚州三级久久电影 | 日本阿v在线资源无码免费 日本阿v片在线播放免费 | 国产囗交10p | 最新国产亚洲亚洲精品视频 | 99久久做夜夜爱天天做精品 | 国产成人免费高清在线观看 | 添加一点爱与你电视剧免费观看 | 亚洲免费黄色 | 中文字幕无码亚洲字幕成A人蜜桃 | 日本一区二区三区在线观看网站 |