Redirect on Ajax Jquery Call
- by Mark Estrada
Hi,
I am newbie to ajax here and I know somebody would have encountered this problem already.
I have a legacy app built on Spring MVC, it has a interceptor(filter) that redirects the user to the login
page whenever there is no session.
public class SessionCheckerInterceptor extends HandlerInterceptorAdapter {
 public boolean preHandle(HttpServletRequest request,
   HttpServletResponse response, Object handler) throws Exception {
  HttpSession session = request.getSession();
  // check if userInfo exist in session
  User user = (User) session.getAttribute("user");
  if (user == null) {
   response.sendRedirect("login.htm");
   return false;
  }
  return true;
 }
}
For non-xmlhttp request, this works fine.. but when I try to use ajax in my application, everything gets weird,
it is not able to redirect to the login page correctly.
As check the value of the
xhr.status = 200  textStatus = parseError  errorThrown = "Invalid JSON -Markup of my HTML Login Page-
$(document).ready(function(){
        jQuery.ajax({
            type: "GET",
            url: "populateData.htm",
            dataType:"json",
            data:"userId=SampleUser",
            success:function(response){
             //code here
            },
         error: function(xhr, textStatus, errorThrown) {
                alert('Error!  Status = ' + xhr.status);
             }
        });
});
I checked on my firebug that there is a 302 HTTP response but I am not sure how to catch the response and redirect the user to the login
page.  Any idea here?  Thanks.