您的位置:首页 > 编程语言 > ASP

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

2016-04-07 20:22 766 查看
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(),得到到的地点则是解码过的。

下面是一些使用例子:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="http://localhost:3281/Js/jquery-1.9.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
var url = "http://www.cnblogs.com/linJie1930906722?test=测试js对URL的编码";
var escape_Url = escape(url);    //escape_Url=http%3A//www.cnblogs.com/linJie1930906722%3Ftest%3D%u6D4B%u8BD5js%u5BF9URL%u7684%u7F16%u7801
var decodeURI_Url = decodeURI(url);  // decodeURI_Url=http://www.cnblogs.com/linJie1930906722?test=测试js对URL的编码
var decodeURIComponent_Url = decodeURIComponent(url); //decodeURIComponent_Url=http://www.cnblogs.com/linJie1930906722?test=测试js对URL的编码
var result = "escape_Url=" + escape_Url + " decodeURI_Url=" + decodeURI_Url + " decodeURIComponent_Url=" + decodeURIComponent_Url;
console.log(result);
alert(result);
</script>
</head>
<body>

</body>
</html>


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: