我有一个简单的登录页面,我正在使用Ajax来检查用户凭据。 如果为true,它应该重定向到另一个页面,否则它应该抛出一条错误消息。
<script type="text/javascript">
function btnSignIn(){
var uName = document.getElementById("uName").value;
var pass = document.getElementById("pass").value;
$.ajax({
url: "/orgNgo/SignIn",
type: "POST",
data: {
"uName" : uName,
"pass" : pass
} ,
success:function() {
//... your other code
//location.reload(); //reload the page on the success
document.getElementById("lblError").innerHTML="Error";
}
});
}
</script>
<table>
<tr>
<td>
UserName:
</td>
<td>
<input type="text" id="username" name="uName"/>
</td>
<td>
<label style="visibility: hidden;">required</label>
</td>
</tr>
<tr>
<td>
Password:
</td>
<td>
<input type="text" id="password" name="pass"/>
</td>
<td>
<label style="visibility: hidden;">required</label>
</td>
</tr>
<tr>
<td colspan="3">
<input type="button" onclick="btnSignIn()" value="Submit"/>
</td>
</tr>
<tr>
<td colspan="3">
Not a member yet? <a href="/orgNgo/SignUp">SignUp</a>
</td>
</tr>
</table>
Servlet代码
if(repeatUsername){//if user name and password matches it comes to this loop
stmt.close();
c.close();
session.setAttribute("lib", uName);
request.getRequestDispatcher("/user/UserHome.jsp").forward(request,response);
//not working
}
else{
String loginError = "UserName or Password is wrong";
request.setAttribute("loginError", loginError);
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(String.valueOf(loginError));
//not working
}
如果用户凭据为true,则页面不会被重定向。当我调试代码时,我发现它在控制台中反映出来。如果用户凭据与之匹配,则loginError String不会传递给Ajax的成功函数。 我已经练习Ajax一个星期了,但通常它只是jsp到servlet但是我在这里因为它是双向的而被卡住了。
提前感谢您的帮助。
答案 0 :(得分:0)
success: function() {
window.location = "another page";
}
将重定向到另一个页面 并且由你来检查credencial