日期:2014-05-16  浏览次数:20698 次

Ajax与Servlet问题
XMLHttp.responseText.trim()=="false":由于出现错误 c00ce56e 而导致此项操作无法完成。

JavaScript代码
HTML code
<script type = "text/javascript" charset="utf-8">
        var XMLHttp ;
        function createXMLHttpRequest(){
            if(window.ActiveObject){
                XMLHttp= new ActiveObject("Microsoft.XMLHTTP");
            }else if(window.XMLHttpRequest) {
                XMLHttp = new XMLHttpRequest();
            }
        }
        function checkUserName(){
            createXMLHttpRequest();
            var name = document.getElementById("uname").value;
            if(name==""){
                alert("用户名不能为空");
                return;
            }
                XMLHttp.open("get","findName?uname="+name,true);
                XMLHttp.onreadystatechange=processRequest;
                XMLHttp.send(null);
            
        }
        function processRequest(){
            if(XMLHttp.readyState==4){
                if(XMLHttp.status==200){
                    if(XMLHttp.responseText.trim()=="false"){
                        alert("存在");
                    }
                    else if(XMLHttp.responseText.trim()=="true"){
                        alert("可用");
                    }
                }
            }
        }
        </script>


Servlet代码
Java code
public class findName extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html;charset = gbk");
        request.setCharacterEncoding("GBK");
        PrintWriter out = response.getWriter();
        String name = request.getParameter("uname");
        UserInfoBiz ubiz = new UserInfoBizImpl();
        int number = ubiz.getUsersByName(name);
        if(number>0){
            out.print(false);
        }
        else{
            out.print(true);
        }
        out.flush();
        out.close();
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        this.doGet(request, response);
    }

}


------解决方案--------------------
看着没什么问题啊
<script type = "text/javascript" charset="utf-8">
会不会是字符编码问题啊
------解决方案--------------------
js的字符串对象没有trim方法,你自己扩展过string对象原型了?
JScript code
String.prototype.trim = function () { return this.replace(/^\s*|\s*$/g, ''); }