日期:2014-05-17  浏览次数:20737 次

Ajax乱码,相当严重。
经测试,是ajax提交到后台的数据时乱码的,是使用str.getBtyes(),这种方式转码也不行。
网上查的方法用遍了还是解决不了。
下面是Ajax的代码
JScript code

function InitAjax(){
         var ajax=false;
         try {
          ajax = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
          try {
           ajax = new ActiveXObject("Microsoft.XMLHTTP");
          } catch (E) {
           ajax = false;
          }
         }
         if (!ajax && typeof XMLHttpRequest!='undefined') {
          ajax = new XMLHttpRequest();
         }
         return ajax;
        }
function sendMsg(){
            if(connection){
                document.getElementById("msgByLine").innerHTML += "<li>您还没有建立连接.</li>";
                return;
            }
            var msg = document.getElementById("text").value;
            if(msg == ""){
                return;
            }
            document.getElementById("text").value = "";
            var url = "servlet/Online?flag=2&id="+flag+"&content="+ msg +"&da="+new Date();
            var ajax = InitAjax();
            url = encodeURI(url);
            url = encodeURI(url);
            ajax.open("GET", url, true);
            //ajax.setRequestHeader("Content-Type","text/html;charset=UTF-8");
            //ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
            //ajax.setRequestHeader("content-Type","text/html;charset=utf-8;"); 
            ajax.onreadystatechange = function() {
                if (ajax.readyState == 4 && ajax.status == 200) {
                    var responseT = ajax.responseText;
                    if(responseT == "over"){
                        document.getElementById("msgByLine").innerHTML += "<li>您已经结束对话·</li>";
                    }else{
                        var d = new Date();
                        var hours = add_zero(d.getHours())+":";
                        var minutes = add_zero(d.getMinutes())+":";
                        var seconds=add_zero(d.getSeconds());
                        document.getElementById("msgByLine").innerHTML += "<li><b>我 "+ hours + minutes + seconds +":</b>&nbsp;&nbsp;"+msg+"</li>";
                    }
                    var div = document.getElementById("message");
                    div.scrollTop = div.scrollHeight;
                }
            }
            ajax.send(null);


下面是Servlet处理的方法
Java code

if(flag.equals("2")||flag.equals("1")){
            int random = Integer.parseInt(request.getParameter("id"));
            if(chat.get(random) == null){
                //已经结束对话
                out.print("over");
                return;
            }
            //检查聊天记录是否超过100条,则删除前50条
            if(chat.get(random).size() > 100)
                for(int i = 0; i < 50; i++){
                    chat.get(random).remove(i);
                }
            //处理中文乱码
            String d = request.getParameter("content");
            System.out.println(request.getQueryString());
            d = new String(d.getBytes("ISO-8859-1"),"UTF-8");
            //System.out.println(d);
            //String content = new String(d.getBytes("ISO-8859-1"),"utf-8");
            //System.out.println(d);
            //String dd = new String(d.getBytes("ISO-8859-1"),"UTF-8");
            //System.