title: how to redirect to another page during a ajax call
body: <p>I have a simple login page and I am using Ajax to check the user credentials.
  if true it should redirect to another page, else it should throw an error message.</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
    function btnSignIn(){
        var uName = document.getElementById("uName").value;
        var pass = document.getElementById("pass").value;
         $.ajax({
             url:   "/orgNgo/SignIn",
             type: "POST",
             data: {
                 "uName" : uName,
                 "pass" : pass
             }  ,
             success:function() {
                 //... your other code
                 //location.reload(); //reload the page on the success
                 document.getElementById("lblError").innerHTML="Error";
             }  
  
         });
    }
    &lt;/script&gt;
  &lt;table&gt;
          &lt;tr&gt;
              &lt;td&gt;
              UserName:
              &lt;/td&gt;
              &lt;td&gt;
              &lt;input type="text" id="username" name="uName"/&gt;
              &lt;/td&gt;
              &lt;td&gt;
              &lt;label style="visibility: hidden;"&gt;required&lt;/label&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
           &lt;tr&gt;
             &lt;td&gt;
              Password:
              &lt;/td&gt;
              &lt;td&gt;
              &lt;input type="text" id="password" name="pass"/&gt;
              &lt;/td&gt;
              &lt;td&gt;
              &lt;label style="visibility: hidden;"&gt;required&lt;/label&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
           &lt;tr&gt;
              &lt;td colspan="3"&gt;
              &lt;input type="button" onclick="btnSignIn()"  value="Submit"/&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
           &lt;tr&gt;
              &lt;td colspan="3"&gt;
              Not a member yet? &lt;a href="/orgNgo/SignUp"&gt;SignUp&lt;/a&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
  </code></pre>
  
  <p>Servlet code</p>
  
  <pre><code>    if(repeatUsername){//if user name and password matches it comes to this loop
                      stmt.close();
                      c.close();
                      session.setAttribute("lib", uName);
    request.getRequestDispatcher("/user/UserHome.jsp").forward(request,response);
                       //not working
                  }
                  else{
  
                      String loginError = "UserName or Password is wrong";
                       request.setAttribute("loginError", loginError);
                      response.setContentType("text/plain");
                      response.setCharacterEncoding("UTF-8");
                      response.getWriter().write(String.valueOf(loginError));
      //not working
                  }
  </code></pre>
  
  <p>Here if the user credentials are true the page is not getting redirected. when I debugged the code I found that it is reflecting in the console. And if the user credentials doesn't match it loginError String isn't getting passed to the success function of Ajax. 
  I'm practising Ajax for a week but usually it would be just jsp to servlet but here I'm stuck because its a two way thing.</p>
  
  <p>Thanks in advance for helping out. </p>