例子
$(document).ready(function(){
$("button").click(function(){
$.post("demo_test_post.asp",
{
name:"Donald Duck",
city:"Duckburg"
},
function(data,status){
alert("Data: " + data + "\nStatus: " + status);
});
});
});
發送POST資料并擷取傳回結果
ajax post送出資料亂碼
firefox下測試都正常,chrome和ie核心下都是有此問題,
此問題設定了頁面屬性為utf-8時候,隻有firefox是傳的charset=utf-8的頭檔案
chrome和ie都沒有指定,是以出現亂碼問題.
解決方法:
$.ajaxSetup({
contentType: "application/x-www-form-urlencoded; charset=utf-8"
});
$.post("test.php", { name: "i5a6", time: "2pm" },
function(data){
process(data);
}, "json");
或者使用:
$.ajax({
url:url,
type:"POST",
data:data,
contentType:"application/x-www-form-urlencoded; charset=utf-8",
dataType:"json",
success: function(){
...
}
})
總結一句:ajax亂碼就是中文問題了,隻要把頁面編輯轉成uft8 就可以了,通常可以使用contentType:來定義類型了,當然在接受時也可以使用轉換函數來操作