I am experiencing issues when trying to compare the response received from a JSP page with Ajax. I am looking to execute an action in the success Ajax function if the out.print() method prints a success message. Despite comparing the data, it seems like it's not functioning correctly as it always redirects to the else section. Since I am new to Ajax, can someone please assist me in resolving this problem?
$.ajax({
url: "login.jsp",
type: "POST",
data: datatopost,
success: function(data){
if(data.search("success") != -1){
console.log(data.data);
$('#loginmessage').html(data);
$("#spinner").css("display", "none");
$("#loginmessage").slideDown();
} else {
$("#spinner").css("display", "none");
$("#loginmessage").slideDown();
}
},
error: function(){
$("#loginmessage").html("<div class='alert alert-danger'>There was an error with the Ajax Call. Please try again later.</div>");
$("#spinner").css("display", "none");
$("#loginmessage").slideDown();
}
});
JSP code...
<%@ include file="connection.jsp"%>
<%
String email=request.getParameter("loginemail");
String loginpass=request.getParameter("loginpassword");
ResultSet rs=st.executeQuery("select password from users where email='"+email+"'");
if(rs.next())
{
String password1=rs.getString(1);
if(password1.equals(loginpass))
{
session.setAttribute("email",email);
out.print("success");
}
else
{
out.print("invalid combination of email and password");
}
} else
{out.print("this account does not have information");
}
%>