AJAX中GET和POST方式乱码解决方案

GET方式
页面:
 var tuiName=document.getElementById('demo_tempaltename').value;
 var url="card.do?method=findTableUiDictByUiname&tuiname="+tuiName;
 createXmlHttp();                        //创建XmlHttpRequest对象
 xmlHttp.onreadystatechange = writeMsg;
 xmlHttp.open("GET", url, true);
 xmlHttp.send(null);
服务器端:
String uiname  = new String(request.getParameter("tudname").getBytes("ISO-8859-1"), "GBK");



POST方式
页面:
    var tuiName=document.getElementById('demo_tempaltename').value;
    var querystring=encodeURI(encodeURI("method=findTableUiDictByUiname&tuiname="+tuiName));
    var url="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乱码问题

你可能感兴趣的:(Ajax)