js 编码、解码与asp.net 编码、解码

js对URL提供:escape,encodeURI,encodeURIComponent 的编码方法encodeURIComponent:推荐使用,它是将中文、韩文等特殊字符转换成utf-8格式的url编码,以是假定给背景转达参数必要利用encodeURIComponent时必要背景解码对utf-8撑持(form中的编码体例和当前页面编码体例不异)


1、escape:不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z
2、encodeURI:不编码字符有82个:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,A-Z
3、encodeURIComponent:不编码字符有71个:!, ',(,),*,-,.,_,~,0-9,a-z,A-Z

JS编码:var result=encodeURIComponent(unit);
asp.net解码:Server.UrlDecode(Request["result"]);

js函数:
encodeURI("url")//编码
decodeURI("url")//解码

asp.net的函数:
Server.UrlEncode("url")//编码
Server.UrlDecode("url")//解码

JS对URL中的特殊字符的URL编码,函数是encodeURIComponent,这个函数编码等于asp.net中的Server.UrlEncode体例。
在asp.net中,利用Request.QueryString[""].ToString()可以直接对编码后的字符串进行解码,也可利用Server.UrlDecode体例进行解码。
在asp.net中,可利用Request.Url.OriginalString来得到URL,利用Request.Url.ToString(),得到到的地点则是解码过的。

下面是一些使用例子:

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4     <title></title>
 5     <script src="http://localhost:3281/Js/jquery-1.9.1.min.js" type="text/javascript"></script>
 6     <script type="text/javascript">
 7         var url = "http://www.cnblogs.com/linJie1930906722?test=测试js对URL的编码";
 8         var escape_Url = escape(url);    //escape_Url=http%3A//www.cnblogs.com/linJie1930906722%3Ftest%3D%u6D4B%u8BD5js%u5BF9URL%u7684%u7F16%u7801
 9         var decodeURI_Url = decodeURI(url);  // decodeURI_Url=http://www.cnblogs.com/linJie1930906722?test=测试js对URL的编码 
10         var decodeURIComponent_Url = decodeURIComponent(url); //decodeURIComponent_Url=http://www.cnblogs.com/linJie1930906722?test=测试js对URL的编码
11         var result = "escape_Url=" + escape_Url + " decodeURI_Url=" + decodeURI_Url + " decodeURIComponent_Url=" + decodeURIComponent_Url;
12         console.log(result);
13         alert(result);
14     </script>
15 </head>
16 <body>
17 
18 </body>
19 </html>
View Code
原文地址:https://www.cnblogs.com/linJie1930906722/p/5365282.html