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("");
通常可以使用这些工具类来编码、解码。乱码一般都是因为发送方和接收放使用的编码不一致造成的,在解码过程中加上正确的编码即可。