I'm new with struts 1 and jQuery. I want to make a login using an AJAX request. This is my Jquery:
$.ajax({
url : "login.do",
type: "POST",
dataType: "text",
data:{
//Got the values before
"user" : user,
"pass": pass
},
success: function(result){
if(result === "OK")
window.location = "hello.do";
}
});
My struts-config:
<action path="/login" type="com.arquitectos.struts.LoginAction" parameter="login">
<forward name="OK" path="/"/>
</action>
My LoginAction:
public class LoginAction extends MappingDispatchAction{
private final String error = "ERROR";
private final String success = "OK";
private final String session = "SESSION";
private Usuario user = new Usuario();
public ActionForward login(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
response.setContentType("text/texyt; charset = utf-8");
response.setHeader("Cache-Control", "no-cache");
try{
String u = request.getParameter("user");
String p = request.getParameter("pass");
if(u != null && p != null)
if(!(u.equals("")) && !(p.equals(""))){
user = TblLogin.Login(u, p);
if(user!=null)
return mapping.findForward(success);
}
}catch(Exception ex){
}
finally{
}
return mapping.findForward(null);
}
The tbl file just execute the query and stuff... Now, my doubt is: how does the AJAX gets the "OK" from the forward? How to get that data and compare it in the jQuery.
I found the solution. It took me a while to figure it out. I was looking in the wrong place. I don't have to try to get data from the forwards.
A out.write("OK") is enough. That's the way my ajax request will get the answer.