title: Trying to catch Log failure and 404 with JQuery
body: <p>I am working on a web Spring MVC 4 application for my end of studies project, the application should connect <strong>web</strong> and <strong>android</strong> clients.</p>
  
  <p>I used <code>@RestController</code> to help android app communicate easily via json. I am trying to catch connection failure ex:<strong>404</strong> in both web or mobile app, even in the web application i tried to make almost all communication with the server via Ajax hoping to make user more relaxed (Progress bars, Live info hints...) but, one of the problems in countries that after using <code>event.preventDefault()</code> from jQuery, i can't get over it in next actions and the response pages loaded and kept unseen! for example here is my login page example:</p>
  
  <p><strong><em>login.js</em></strong></p>
  
  <pre><code>jQuery(document).ready(function ($) {
      $("#login-form").submit(function (event) {
          //Do somthing...
          // Call login function
          logViaAjax(event);
          // Prevent the form from submitting via the browser.
          return false; //Or using event.preventDefault()
      });
  });
  
  // logViaAjax function
  function logViaAjax(event) {
      var logObj = {};
      logObj["username"] = $("#username").val();
      logObj["password"] = $("#password").val();
      //the response
      $.ajax({
          type: "POST",
          contentType: "application/x-www-form-urlencoded; charset=UTF-8",
          url: "/login",
          data: logObj,
          dataType: 'text',
          timeout: 10000,
      success: function (data) {
          if (data.indexOf("login-box") !== -1) {
              showLogFail();
          }else{
              window.location.reload();
              //I dont need to reload the page because this gives me login page again... May be if there is a way to get the URL from data!?
          }
      },
      error: function () {
          showWarning();
      }
      });
  </code></pre>
  
  <p>I am using <code>Spring Security 4</code> to handle authentification and we know that it gives url ex:<code>/login?fail</code> for failure and get the requested page if succeed and i recently knew that it redirect even before getting in the loginUrl my ex <code>/login</code>. I tried to test if the user logged in on server side and send json response but this could leads to lose the page before requesting login.</p>
  
  <p>I think i should test in the <strong><code>success</code></strong><code>: function()</code> if url loaded in the jQuery(but not showed) contains <code>?fail</code> or not, to inform user to change log or password. Else, if it do not, the page user need is charged in the back and all what i need is to show it.</p>
  
  <hr>
  
  <h1>UPDATE</h1>
  
  <p>After those little changes all what I need to make is just to keep the page beyond the login page and reshow it. Take note that this function is automatically offered by Spring Security but it still running on background. Ex in my case when user click <code>/messenger</code> the app redirect him to <code>/login</code> to login, if he fail login it gives <code>/login?fail</code> but this page is unseen due to JQuery, else he login successfully it send him to <code>/messenger</code> again…</p>