When using Ajax to develop a website, often have friends encounter garbled problems, and it is difficult to find a solution. In fact, to solve the problem of Ajax Chinese garbled is very simple.
1. Service-side program:
<%
liststr="AJAX中文乱码的简单解决方法"
sponse.write escape(liststr) '用escape编码
%>
2. Client JavaScript Program
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);
}
}
Through escape and unescape can solve Ajax Chinese garbled, extrapolate, this method can not only solve Ajax Chinese garbled, encounter other garbled problem can also use this method.