1. 程式人生 > >js對字串編碼,解碼 .net對字串編碼,解碼

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("");
通常可以使用這些工具類來編碼、解碼。亂碼一般都是因為傳送方和接收放使用的編碼不一致造成的,在解碼過程中加上正確的編碼即可。