引入:
今天在研究ajax調用服務器一般處理程序的時候,發現中文亂碼的問題。於是着手研究一下js端 和服務器端對html的編碼和解碼方式。
對於ajax中碰到的問題是: 在url傳值中參數包含中文。 傳到服務器時無法解析中文導致亂碼。后來找了些資料發現js會自動編碼。
於是 我這樣做:
<script type="text/javascript"> //var data=<%=data %> jQuery(document).ready(function ($) { $("ul.menu").dropmenu(); var title = encodeURI('聯系我們'); $.ajax({ type: "GET", url: "http://www.cnblogs.com/Controls/Handler1.ashx?title=" + title + "&value=TopicContent", //我們用text格式接收 dataType: "application/x-www-form-urlencoded; charset=utf-8", data: "meth=load", success: function (msg) { alert(msg); //顯示后台數據 $(".contact").html(msg); } }); </script>
然后在服務器端:
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string _title =""; if( HttpContext.Current.Request.QueryString["title"]!=null&&HttpContext.Current.Request.QueryString["value"]!=null) { _title = HttpContext.Current.Request.QueryString["title"]; //如果js未轉換編碼,這里會出現亂碼。反之 得到 聯系我們
string _value = HttpContext.Current.Request.QueryString["value"];//可以直接得到TopicContent } context.Response.Write(_title ); }
其實request.querystring 除了接受不被強制編碼的數據外,反之也是可以解析得到中文的。
再次基礎上我查了些資料希望對同樣處於困惑的你又幫助:
js對文字進行編碼涉及3個函數:escape,encodeURI,encodeURIComponent,相應3個解碼函 數:unescape,decodeURI,decodeURIComponent
1、 傳遞參數時需要使用encodeURIComponent,這樣組合的url才不會被#等特殊字符截 斷。
例如:<script language="javascript">document.write('<a href="http://passport.baidu.com/?logout&aid=7& u='+encodeURIComponent("http://cang.baidu.com/bruce42")+'">退出</a& gt;');</script>
2、 進行url跳轉時可以整體使用encodeURI
例如:Location.href=encodeURI("http://cang.baidu.com/do/s?word=百度& ct=21");
3、 js使用數據時可以使用escape
[Huoho.Com編輯]
例如:搜藏中history紀錄。
4、 escape對0-255以外的unicode值進行編碼時輸出%u****格式,其它情況下 escape,encodeURI,encodeURIComponent編碼結果相同。
最多使用的應為encodeURIComponent,它是將中文、韓文等特殊字符轉換成utf-8格式的url編碼,所以如果給后台傳遞 參數需要使用encodeURIComponent時需要后台解碼對utf-8支持(form中的編碼方式和當前頁面編碼方式相同)
escape不編碼字符有69個:*,+,-,.,/,@,_,0-9,a-z,A-Z
encodeURI不編碼字符有82個:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a- z,A-Z
encodeURIComponent不編碼字符有71個:!, ',(,),*,-,.,_,~,0-9,a-z,A-Z
在使用url進行參數傳遞時,經常會傳遞一些中文名的參數或URL地址,在后台處理時會發生轉換錯誤。在有些傳遞頁 面使用GB2312,而在接收頁面使用UTF8,這樣接收到的參數就可能會與原來發生不一致。使用服務器端的urlEncode函數編碼的URL,與使用 客戶端javascript的encodeURI函數編碼的URL,結果就不一樣。
escape() 方法:
采用ISO Latin字符集對指定的字符串進行編碼。所有的空格符、標點符號、特殊字符以及其他非ASCII字符都將被轉化成%xx格式的字符編碼(xx等於該字符 在字符集表里面的編碼的16進制數字)。比如,空格符對應的編碼是%20。unescape方法與此相反。不會被此方法編碼的字符: @ * / +
encodeURI() 方法:把URI字符串采用UTF-8編碼格式轉化成escape格式的字符串。不會被此方法編碼的字符:! @ # $& * ( ) = : / ; ? + '
encodeURIComponent() 方法:把URI字符串采用UTF-8編碼格式轉化成escape格式的字符串。與encodeURI()相比,這個方法將對更多的字符進行編碼,比如 / 等字符。所以如果字符串里面包含了URI的幾個部分的話,不能用這個方法來進行編碼,否則 / 字符被編碼之后URL將顯示錯誤。不會被此方法編碼的字符:! * ( )
另外,encodeURI/encodeURIComponent是在javascript1.5之后引進的,escape則在 javascript1.0版本就有。
escape() 方法
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."
譯:escape方法以Unicode格式返回一個包含傳入參數內容的string類型的值。 Escape方法會將傳入參數中所有的空格、標點符號、重音字符以及其它任何非ASCII字符替換為%xx的編碼形式,其中xx與其所表示的字符的16進 制數表示形式相同。如空格字符的16進制表示形式為0x20,則此時xx應為20,即escape(‘ ’) 返回“%20”。
Mozilla Developer 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.
譯:escape和unescape方法能夠幫助你編碼和解碼字符串。escape方法對於ISO Latin字符集中的字符組成的參數,返回其16進制編碼。相對應的,unescape方法則能將16進制編碼形式的參數轉化成為其ASCII碼形式。
encodeURI()方法
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.
譯:encodeURI方法返回一個經過編碼的URI。如果將encodeURI方法的編碼結果傳遞給decodeURI方法作參數,則能得到原始 的未編碼的字符串。需要注意到是encodeURI方法不編碼如下字符":", "/", ";", and "?"。如果想要編碼這些字符,請使用encodeURIComponent方法。
Mozilla Developer 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.
譯:通過將每個屬於特定的字符集合的字符替換為一個、兩個或者三個(為什么是“一個、兩個或者三個”本人也沒有搞懂,望高人賜教)使用UTF-8編 碼來表示這個字符的escape序列來編碼一個URI。如 ~!@#$%^&*(){}[]=:/,;?+\''"\\ 將被替換為 ~!@#$%25%5E&*()%7B%7D%5B%5D=:/,;?+''%22%5C
encodeURIComponent()方法
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.
譯:encodeURIComponent方法返回一個編碼過的URI。如果將encodeURIComponent方法的編碼結果傳遞給 encodeURIComponent方法作參數,則能得到原始的未編碼的字符串。因為encodeURIComponent方法會編碼所有的字符,所以 如果待編碼的字符串是用來表示一個路徑(如/dir1/dir2/index.htm)時,就一定要小心使用了。‘/’符號會被其編碼之后,將不再是一個 有效的路徑標識符,所以不能被web服務器正確地識別。當字符串包含一個單獨的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編 碼來表示這個字符的escape序列來編碼一個URIComponent。
有什么區別?何時使用?
通過上面的介紹可以看出,MS的文檔明顯要比Mozilla詳細、易懂一些,但是它們表達的都是一個意思。但是escape(), encodeURI()和 encodeURIComponent()有什么異同,它們分別適用於那種特定的情況呢?
escape方 法並不編碼字符+。而我們知道,在用戶提交的表單字段中,如果有空格,則會被轉化為+字符,而服務器解析的時候則會認為+號代表空格。由於這個缺 陷,escape方法並不能正確地處理所有的非ASCII字符,你應當盡量避免使用escape方法,取而代之,你最好選擇 encodeURIComponent()方法。
escape()不編碼的字符:@*/+
相對於使用escape方法,使用encodeURI方法會顯得更專業一些。當你需要編碼一整個URI的時候,你可以使用此方法,因為URI中的合 法字符都不會被編碼轉換。需要注意到是字符’也是URI中的合法字符,所以也不會被編碼轉換。
encodeURI() 不編碼的字符: ~!@#$&*()=:/,;?+''
encodeURIComponent方法在編碼單個URIComponent(指請求參數)應當是最常用的。需要注意到是字符’也是URI中的合 法字符,所以也不會被編碼轉換。
encodeURIComponent()不編碼的字符: ~!*()''
然而對於serve.htmlencode: msdn是這樣說的:
HTML 編碼確保文本能在瀏覽器中正確顯示,而不是被瀏覽器解釋為 HTML。 例如,如果文本字符串包含小於號 (<) 或大於號 (>),則瀏覽器會把這些字符解釋為 HTML 標記的起始或結束括號。 當字符為 HTML 編碼時,它們將轉換為字符串 < 和 >,以便瀏覽器能夠正確顯示小於號和大於號。
HtmlEncode 方法是一種簡便方式,用於在運行時從 ASP.NET 應用程序訪問 HttpUtility.HtmlEncode 方法。 HtmlEncode 在內部使用 HttpUtility.HtmlEncode 對字符串進行編碼。