0

I m trying to redirect control to next jsp page. If the boolean value is true redirect to success jsp page else redirect to login page.

@RequestMapping(value = "/validate", method = RequestMethod.POST)
@ResponseBody
public ModelAndView(HttpServletRequest request, HttpServletResponse response, Model model) {
    String userName = request.getParameter("userName");
    String password = request.getParameter("password");
    boolean validuser=employeeService.checkLogin(userName,password);
    if(validuser == true)
    {
        model.setViewName("success");
        return model;
    }
    model.setViewName("login");
    return model;
}

This is my Ajax call.

 $(document).ready(function() {
      $("form").submit(function() {
         var userName=$("#inputEmail").val();
         var password=$("#inputPassword").val();
            $.ajax({
                type : "post",
                url : "${pageContext.request.contextPath}/validate",
                data : {userName:userName, password:password},
                success:function(data){
                    alert(data);        
                },
                error:function()
                {
                    alert("Error ");
                }        
                }); 
      });
    });

Control is not going to success block. Only the error alert is getting displayed.

public String validate(HttpServletRequest request, HttpServletResponse response, Model model) {
    String userName = request.getParameter("userName");
    String password = request.getParameter("password");
    boolean validuser=employeeService.checkLogin(userName,password);

    if(validuser == true)
    {
        return "redirect:/success";
    }
    else{
    return "redirect:/login";
    }
}

I have tried it using this approach. Still its entering error block.

abc
  • 29
  • 1
  • 7

2 Answers2

0

Add window.location.href = 'YOUR_REDIRECT_URL' in your success callback.

 success:function(data){
     alert(data);  
     window.location.href = 'YOUR_REDIRECT_URL'; // redirect      
 },
Muthu Kumaran
  • 17,682
  • 5
  • 47
  • 70
0

If it's an ajax call, you can't use the redirect: in the return url. On your controller, return "http://example.domain.com/success". Then in your success handler use window.location.href = 'redirect_url'

greenkode
  • 4,001
  • 1
  • 26
  • 29