Javascript .js代码中给出的document.location()函数在Internet Explorer中运行良好,但在Firefox中不起作用
Javascript .js代码中给出的document.location()函数在Internet Explorer中运行良好,但在Firefox中不起作用,javascript,ajax,internet-explorer,jsp,firefox,Javascript,Ajax,Internet Explorer,Jsp,Firefox,logn.js代码中给出的document.location()函数在Internet Explorer中运行良好,但在Firefox中不起作用。给定的js代码用于在登录页面中实现AJAX。。AJAX将代码定向到一个servlet,如果login OK,该servlet将用户登录作为响应 logn.js function logn(emailId,password) { var parameters="emailId="+emailId+"&password="+password
logn.js
代码中给出的document.location()
函数在Internet Explorer中运行良好,但在Firefox中不起作用。给定的js代码用于在登录页面中实现AJAX。。AJAX将代码定向到一个servlet,如果login OK,该servlet将用户登录作为响应
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);
}
以下是LoginServlet.java的servlet代码
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");
}
}
请尝试以下操作,而不是document.location():
请尝试以下操作,而不是document.location(): 您应该使用:
window.location = "userhome.jsp";
及
你在做这件事时遇到了一些问题。最好使用window.location
而不是document.location
。你给它赋值,而不是像函数一样调用它
MDN参考:您应该使用:
window.location = "userhome.jsp";
及
你在做这件事时遇到了一些问题。最好使用window.location
而不是document.location
。你给它赋值,而不是像函数一样调用它
MDN参考:您是否在firefox中安装了firebug以便查看发生了什么错误?您是否在firefox中安装了firebug以便查看发生了什么错误?window.location和document.location之间有什么区别?@AlanFoster-正如您所发现的,document.location存在跨浏览器兼容性问题,并且在某些浏览器中是只读的。有关更多信息,请参见此处:window.location和document.location之间有什么区别?@AlanFoster-正如您所发现的,document.location存在跨浏览器兼容性问题,并且在某些浏览器中是只读的。有关更多信息,请参见此处:
window.location = "adminhome.jsp";