在使用AJAX開發網站時,經常有朋友遇到亂碼的問題,而且一下子難以找到解決方法。其實解決AJAX中文亂碼問題很簡單。
1、服務端程序:
以下為引用的內容:<%
liststr="AJAX中文亂碼的簡單解決方法"
sponse.write escape(liststr) '用escape編碼
%>
2、客戶端JAVASCRIPT程序
function toserver(url)
{
var req = new XMLHttpRequest();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4 )
{
if(req.status == 200 || req.status == 304) {
getstr=unescape(req.responseText) '用unescape解碼
alert(getstr);
}
else
{return false;}
}
}
req.open('GET', url);
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
req.send(null);
}
}
通過escape與unescape就可以解決AJAX中文亂碼,舉一反三,這種方法不僅可以解決AJAX中文亂碼,碰到其它亂碼問題同樣可以採用此方法