2011年3月29日 星期二

Ajax傳遞中文參數失敗 產生亂碼的解決方案

當使用 Ajax 傳遞中文參數時,小心笨蛋IE又來給你添麻煩了。解決方案很簡單,將欲傳送的中文以 encodeURIComponent() 包起來即可。範例:

$.ajax({
url: "targetAjaxHandler.php",
cache: false,
dataType: "html",
type:"GET",
data: "data="+encodeURIComponent("笨蛋IE")
error: function(xhr) {
},
success: function(response) {
screenHandler();
}
});