js对字符串编码,解码 .net对字符串编码,解码


js对字符串编码的方式:
1.escape();// 对字符串进行编码  
2.encodeurl();//把字符串编码为URI  /
3.encodeURIComponent();//把字符串编码为URI组件
var str = "http://localhost:8080/Product/index?id=123&attr=456&area=中国";
console.log(encodeURI(str)); //(只编码了中文)不会对:/?&等url中起分割作用的字符进行编码;
console.log(encodeURIComponent(str)); //(编码了:/?=&中文)
console.log(escape(str));//(编码了:?=&中文)中文编码后和上面两种不一样!w3school解释是,escape函数会对asci码中字母、数字及符号(*@-_+./)之外的所有字符进行编码。
----------
http://localhost:8080/Product/index?id=123&attr=456&area=%E4%B8%AD%E5%9B%BD
http%3A%2F%2Flocalhost%3A8080%2FProduct%2Findex%3Fid%3D123%26attr%3D456%26area%3D%E4%B8%AD%E5%9B%BD 
http%3A//localhost%3A8080/Product/index%3Fid%3D123%26attr%3D456%26area%3D%u4E2D%u56FD
----------------------------
js的编码,解码,asp.net(c#)对应的解码,编码;
1.js:escape();unescape(); C#:HttpUtility.UrlEncode();HttpUtility.UrlDecode();
2.js:encodeURI();decodeURI(); C#:decodeURIComponent();
3.js:encodeURIComponent(); decodeURIComponent();  C#:[HttpContext.Current]Server.UrlEncode(); [HttpContext.Current]Server.UrlDecode(); 
--------------------------------
在web开发中可能经常遇到的是url编码、解码的问题,或者url参数乱码等等。。。
Server.UrlEncode("");
Server.UrlDecode("");
System.Web.HttpUtility.UrlEncode("");
System.Web.HttpUtility.UrlDecode(""); 
System.Uri.EscapeDataString("");
System.Uri.UnescapeDataString("");
通常可以使用这些工具类来编码、解码。乱码一般都是因为发送方和接收放使用的编码不一致造成的,在解码过程中加上正确的编码即可。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值