.js代码中给出的document.location()函数在Internet Explorer中工作正常,但在Firefox中不起作用

时间:2012-03-04 19:12:21

标签: javascript ajax internet-explorer jsp firefox

document.location()代码中给出的

logn.js函数在Internet Explorer中工作正常,但在Firefox中不起作用。给定的js代码用于在登录页面中实现AJAX .AJAX将代码定向到servlet,如果登录OK,则将用户Login作为响应。

logn.js

function logn(emailId,password) {
    var parameters="emailId="+emailId+"&password="+password;
    var xmlhttp;

    if (window.XMLHttpRequest) {    // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else {    // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {     
            if(xmlhttp.responseText.toString()=="User Login") {     
                document.location("userhome.jsp"); 
            } else if(xmlhttp.responseText.toString()=="Admin Login") { 
                document.location("adminhome.jsp"); 
            }else {
                //document.getElementById("message").innerHTML = xmlhttp.responseText;
                alert(xmlhttp.responseText);
            }       
        }
    };

    xmlhttp.open("POST", "LoginServlet", true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send(parameters);   
}

以下是servlet代码LoginServlet.java

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out=response.getWriter();
        String emailId=request.getParameter("emailId");
        String password=request.getParameter("password");



        if (emailId.isEmpty()||password.isEmpty()) { 
            out.write("Please enter EmailId/Password");
        } else {
            LoginModel lm=new LoginModel();
            lm.setEmailId(emailId);
            lm.setPassword(password);

            LoginService ls=new LoginService();
            lm=(LoginModel) ls.loginCheck(lm);

            if(lm!=null){
                System.out.println("login ok");
                HttpSession session =request.getSession();
                System.out.println(lm.getLoginId());
                session.setAttribute("userlogin", lm);

                if (lm.getIsAdmin()==0) {
                    System.out.println("aaaaaaaaaaa");
                    out.write("User Login");
                }
                else if (lm.getIsAdmin()==1) 
                    out.write("Admin Login");

                ls.setIsActive(lm.getLoginId(),1);
            } else 
                out.write("Wrong EmailId/Password");
        }
    }

2 个答案:

答案 0 :(得分:2)

您应该使用:

window.location = "userhome.jsp";

window.location = "adminhome.jsp";

你如何做这件事有几个问题。最好使用window.location代替document.location。并且,您分配给它,而不是像函数一样调用它。

MDN参考:https://developer.mozilla.org/en/DOM/window.location

答案 1 :(得分:1)

而不是document.location()尝试:

document.location.href = ...