我有一个简单的登录来测试AJAX
继承我的片段:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login Test Page</title>
<script src="../js/jquery-1.10.1.min.js" ></script>
<script type="text/javascript">
$('input[type="submit"]').click(function(e) {
e.preventDefault();
var action_script = $(this).closest('form1').prop('action');
$.post(action_script, {
userName : $(this).closest('form1').find('input[name="userName"]'),
password : $(this).closest('form1').find('input[name="password"]')
}, function(returned_data) {
//this part executes once the server returns a successful response
alert("Please fill UserName & Password!");
});
});
</script>
</head>
<body>
<form action="LoginController" method="post">
<!-- Login body -->
<table>
<tr>
<td><label for="userName">Username:</label></td>
<td><input type="text" name="userName" /></td>
</tr>
<tr>
<td><label for="password">Password:</label></td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td><input type="submit" /></td>
</tr>
</table>
</form>
</body>
和一个控制器
UserDAO userDAO = new UserDAO(); // instantiate database
String userName = request.getParameter( "userName" ); // get userName String from the Login.jsp
String password = request.getParameter( "password" ); // get password String from the Login.jsp
if( userDAO.authenticate( userName, password ) ) // validate userName and password
{
UserModel userModel = userDAO.getUserDetails( userName ); // get userModel that correspond to userName parameter
request.getSession().setAttribute( "userName", userName ); // set SESSION REQUEST to be forward to MainPage.jsp
request.setAttribute( "userDetails", userModel ); // set REQUEST to be forward to MainPage.jsp
RequestDispatcher rd = request.getRequestDispatcher( "MainPage.jsp" );
rd.forward( request, response ); // forward request to MainPage.jsp
return;
}
else
{
response.sendRedirect( "Login.jsp" );
return;
}
我想要做的是如果登录失败,我想保留用户名和密码来测试它是如何AJAX
工作的,如果可能的话也可以测试,JSON
但我还不知道我应该在哪里JSON
进行测试。
我不知道我做错了什么,这是我第一次进行网络开发,请指导我。