2012-03-04 61 views
0

document.location()logn.js中給出的函數在Internet Explorer中正常工作,但在Firefox中不起作用。給定的js代碼是用於在登錄頁面中實現AJAX的。AJAX將代碼指向一個servlet,如果登錄成功,則將用戶登錄作爲響應。.js代碼中給出的document.location()函數在Internet Explorer中正常工作,但在Firefox中不起作用

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"); 
     } 
    } 
+0

你已經安裝在Firefox螢火所以看到什麼錯誤發生? – peroija 2012-03-04 19:13:49

回答

2

你應該使用:

window.location = "userhome.jsp"; 

window.location = "adminhome.jsp"; 

有與你是如何做一對夫婦的問題。最好使用window.location而不是document.location。而且,你指定它,而不是像一個函數那樣調用它。

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

+0

window.location和document.location有什麼區別? – AlanFoster 2012-03-04 19:17:03

+0

@AlanFoster - 正如您發現的那樣,document.location存在跨瀏覽器兼容性問題,並且在某些瀏覽器中是隻讀的。在這裏看到更多的信息:http://stackoverflow.com/questions/2430936/whats-the-difference-between-window-location-and-document-location-in-javascrip – jfriend00 2012-03-04 19:22:02

+0

謝謝@ jfriend00 ...它的工作:) – Sighil 2012-03-04 19:29:22

1

代替document.location()嘗試:

document.location.href = ... 
+0

thanx bro ..這個代碼的作品:) – Sighil 2012-03-04 19:30:36

+0

當然它,;-) – Tom 2012-03-04 19:33:31

相關問題