我必须使用ajax将数据从登录表单发送到loginServlet。 该servlet正常工作,ajax调用该servlet。问题是ajax没有进入成功功能,永远不会显示alert(data)。 请帮助我
表单代码
<form style="background-color:white" onsubmit="login();"> <!-- -->
<!-- Tenere per la registrazione di un nuovo account <div class="form-group"><label for="subject">Subject</label><select class="form-control" id="subject"><option value="" selected="">Choose Subject</option><option value="1">Subject 1</option><option value="2">Subject 2</option><option value="3">Subject 3</option></select></div> -->
<div class="form-group"><label for="email">Email</label><input class="form-control" type="email" name = "email" id="email" placeholder="User Name" ></div>
<div class="form-group"><label for="password">Password</label><input class="form-control" type="password" name = "password" id="password" placeholder="Password"></div>
<div class="form-group">
<div class="form-row">
日期
ajax代码
function login(){
var email =$("#email").val();
var password =$("#password").val();
$.ajax({
type: "POST",
url: "signin",
datatype:"JSON",
data:{em:email,
pass:password
},
success: function(data){
alert(data);
if(data=="ok")
$(location).attr("href","account.jsp");
}
});
}
Servlet代码
String password=request.getParameter("pas");
String email=request.getParameter("em");
System.out.println(email+" "+password);
UtenteDao dao=DatabaseManager.getInstance().getDaoFactory().getUtenteDao();
System.out.println("utente dao creato");
HttpSession session=request.getSession();
System.out.println("ricevuta la sessione");
Utente u =dao.getByPrimaryKey(email, password);
if(u==null) {
response.getOutputStream().print("no");
}
else {
response.getOutputStream().print(email+"post");
session.setAttribute("nome", u.getNome());
session.setAttribute("cognome", u.getCognome());
session.setAttribute("email", u.getEmail());
System.out.println(u.getSaldo());
session.setAttribute("saldo", u.getSaldo());
session.setAttribute("logged", true);
//response.sendRedirect("account.jsp");
response.getOutputStream().print("ok");
}