ajax服务器端到客户端乱码问题

服务器端使用的是Servlet,传递的中文都是乱码,

 public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int parent=Integer.parseInt(request.getParameter("parent"));
        response.setContentType("text/xml");
        request.setCharacterEncoding("GBK");
        response.setCharacterEncoding("GBK");
        MenuBiz menuBiz=new MenuBiz();
        ArrayList<Menu> menuList=menuBiz.loadMenu(parent);
        StringBuffer menuName=new StringBuffer();
        for (Menu menu : menuList) {            
            menuName.append(URLEncoder.encode(menu.getMenuName(),"GBK")+"-");
        }

        PrintWriter out=response.getWriter();

        out.println(menuName);
        out.flush();
        out.close();

        //session.setAttribute("menuList", menuList);

    }

客户端用的是js

 <script type="text/javascript">
        var xmlHttp;
        var mid;
        function menu(menuId){
            mid=menuId.id;
            var url="MenuServlet?parent="+mid;          
            xmlHttp=createXmlHttpRequest();         
            xmlHttp.onreadystatechange=processRequest;          
            xmlHttp.open("GET",url,true);
            xmlHttp.send(null);
        }

        function processRequest(){
            if(xmlHttp.readyState==4){
                if(xmlHttp.status==200){                
                    var sobj=document.getElementById(mid);
                    var ht=sobj.innerHTML;
                    sobj.innerHTML=ht;
                    var ceshi= xmlHttp.responseText.split("-");

                    var suggest="";
                    if(ceshi.length>0&&ceshi[0].length>0){
                        for(var i=0;i<ceshi.length;i++){                         
                            suggest+="<div>"+ceshi[i]+"</div>";
                        }
                        sobj.innerHTML+=suggest;
                    }
                }else{
                    alert("请求处理返回的数据有误");
                    alert(xmlhttp.status);
                }
            }
        }

        function createXmlHttpRequest(){
            if(window.ActiveXObject){
                return new ActiveXObject("Microsoft.XMLHTTP");
            }else if(window.XMLHttpRequset){
                return new XMLHttpRequset();
            }
        }
    </script>
查看全部
u011321910
万象归一
2015/09/08 09:03
  • 乱码
  • ajax
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

1个回复