国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當前位置: 首頁 - 科技 - 知識百科 - 正文

JSURL傳中文參數引發的亂碼問題_javascript技巧

來源:懂視網 責編:小采 時間:2020-11-27 20:44:11
文檔

JSURL傳中文參數引發的亂碼問題_javascript技巧

JSURL傳中文參數引發的亂碼問題_javascript技巧:解決方法如下: 1、在JS里對中文參數進行兩次轉碼 代碼如下: var login_name = document.getElementById(loginname).value; login_name = encodeURI(login_name); login_name = encodeURI(login_na
推薦度:
導讀JSURL傳中文參數引發的亂碼問題_javascript技巧:解決方法如下: 1、在JS里對中文參數進行兩次轉碼 代碼如下: var login_name = document.getElementById(loginname).value; login_name = encodeURI(login_name); login_name = encodeURI(login_na

解決方法如下:

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

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

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

JSURL傳中文參數引發的亂碼問題_javascript技巧

JSURL傳中文參數引發的亂碼問題_javascript技巧:解決方法如下: 1、在JS里對中文參數進行兩次轉碼 代碼如下: var login_name = document.getElementById(loginname).value; login_name = encodeURI(login_name); login_name = encodeURI(login_na
推薦度:
標簽: 亂碼 js 編碼
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 一区二区视频在线播放 | 欧美亚洲一二三区 | 成人区精品一区二区不卡亚洲 | 久久久性 | 欧美 日韩 亚洲另类专区 | 亚洲欧美日韩综合在线播放 | 一级毛片不收费 | 欧美一区二区日韩一区二区 | 国产偷亚洲偷欧美偷精品 | 欧美精品一区二区三区四区 | 精品日韩欧美国产一区二区 | 欧美激情视频一区 | 欧美日韩色视频在线观看 | 欧美 亚洲 中文字幕 | 欧美第五页| 欧美色综合图区 | 国产精品一页 | 国内精品一区二区三区 | 国产欧美日韩中文字幕 | 一级毛片免费毛片一级毛片免费 | 欧美福利专区 | 精品久久综合一区二区 | 日韩伦理网 | 久草天堂| 日韩欧美在线观看成人 | 日本久久香蕉一本一道 | 欧美激情二区 | 中文国产成人精品久久一区 | 伊人网影院 | 日韩经典第一页 | 亚洲区欧美 | 国产不卡精品一区二区三区 | 亚洲伊人网站 | 欧美成人一区二区三区在线视频 | 久久er | 人人草视频在线观看 | 日日夜夜草 | 亚洲黄色一区二区 | 性xxxxfreexxxxx国产 | 亚洲国产成人精品女人久久久 | 日韩一页 |