AJAX中GET和POST方式乱码解决方案
GET方式
页面:
vartuiName=document.getElementById('demo_tempaltename').value;
varurl="card.do?method=findTableUiDictByUiname&tuiname="+tuiName;
createXmlHttp();//创建XmlHttpRequest对象
xmlHttp.onreadystatechange=writeMsg;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
服务器端:
Stringuiname=newString(request.getParameter("tudname").getBytes("ISO-8859-1"),"GBK");
POST方式
页面:
vartuiName=document.getElementById('demo_tempaltename').value;
varquerystring=encodeURI(encodeURI("method=findTableUiDictByUiname&tuiname="+tuiName));
varurl="card.do";
createXmlHttp();//创建XmlHttpRequest对象
xmlHttp.onreadystatechange=writeMsg;
xmlHttp.open("POST",url,true);
xmlHttp.setRequestHeader("content-length",querystring.length);
xmlHttp.setRequestHeader("Connection","close");
xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlHttp.send(querystring);
服务器端:
String uiname =URLDecoder.decode(request.getParameter("tuiname"),"UTF-8");实践证明,这两种方式完全能够解决AJAX乱码问题