1.產生亂碼原因:表單提交使用的method="get",get方式數據都是通過地址欄傳輸,數據會以iso-8859-1方式傳輸,因此產生亂碼
2.概念:URI: Uniform ResourceIdentifiers,通用資源標識符,有效的URI中不能包含某些字符,例如空格
3.解決編碼和解碼的方法:
(1)js中decodeURIComponent() 函數的定義與用法:
定義:decodeURIComponent() 函數編碼的URI進行解碼[encodeURIComponent() 可以對字符串編碼]
語法:decodeURIComponent(uri)
(2)js中encodeURI()函數的定義與用法:
定義:encodeURI()函數可以把字符串作為URI進行編碼,在URI中具有特殊含義的ASCII標點符號,encodeURI()函數是不會進行轉義的【decodeURI()可以解碼URI】
語法:encodeURI(uri)
4.方法小結一下:
相同點:
(1)encodeURI(),和encodeURIComponent()是對字符進行編碼。
(2)decodeURI(),和decodeURIComponent()是對相應編碼過的字符進行解碼。
區別:
方法中包含的URI()的編碼和解碼不會對本身屬於URI的特殊字符進行編碼,例如冒號等;
方法中包含URICompent()的編碼和解碼則會對它發現的任何非標准字符進行編碼進行編碼;
5.Demo
<form id="test" accept-charset="utf-8" onsubmit="document.charset='utf-8'">
<input type="text" id="name">
<input type="text" id="age">
</form>
通過調用ajax:
$("#test").form("submit",
{
url:encodeURI("/A/Bmethod"+new Date().getTime()),//有時間函數不轉義加上encodeURI
onSubmit:function(p){
p.name=$("#name").val(),
p.age=$("#name").
}
},
success:function(data)
{
data.imageSrc=decodeURIComponent(data.imageurl);//對imageurl進行解密
}
)