title: Grails Spring Security AJAX responds with HTML of requested page, not JSON from ajaxSuccess()
body: <p>So I have been playing with Grails' Spring Security, I'm having a great time but I'm stuck on one little thing regarding logging in via AJAX.  I have a login form that posts to <code>/j_spring_security_check</code> via AJAX in order to log in, it is working very well, but I am puzzled by the different responses when I get to the login page 1) directly, 2) when I am redirected to the login page when I try to access a secured page. The login page is <code>/login/auth</code>, I replaced the standard form in <code>auth.gsp</code> with an AJAXified form.  Here are the two situations:</p>
  
  <ol>
  <li>I browse to <code>/login/auth</code> directly, when I do this and log in with AJAX I get the standard JSON object back, that is I get something like <code>{"success":true,"username":"charles"}</code> or <code>{"error":"Sorry, we were not able to find a user with that username and password."}</code> - no surprises - all is well.</li>
  <li>Since the <code>/login/auth</code> is "the" log in page when I browse to another controller/action (like <code>/users/list</code>) in my app and I am not authenticated I get redirected to <code>/login/auth</code>, as expected.  So when I enter in bogus credentials I get back <code>{"error":"Sorry, we were not able to find a user with that username and password."}</code>, as expected, but when I log in with correct credentials I get back the actual HTML of the originally requested page in the response body.  Ideally, I would like to get back something like <code>{"success":true,"username":"charles", referer:"/users/list"}</code> - so that my login form can do cool animationy stuff then redirect to the originally requested URL.  I see what's going on, in situation number 1 <code>ajaxSuccess()</code> in <code>LoginController.groovy</code> gets called (which returns the JSON) - but in this situation it does not get called, and I can't find out what does get called so that I can hack it to do what I want.</li>
  </ol>
  
  <p>Is there a better way to do this?  Or can somebody point me in the right direction?  I would greatly appreciate any help!</p>
  
  <p>Thanks a lot!</p>
  
  <p>:)</p>
  
  <p>Here's my AJAX call if it helps, although it is very standard:</p>
  
  <pre><code>&lt;script&gt;
  $(document).ready(function(){
      $("#submit").click(function(){
          $("#response").html("Attempting login...");
          var formdata = $('#loginForm').serialize();
          $.ajax({
              url: "/UserDemo/j_spring_security_check",
              type: 'post',
              data: formdata,
              success: function(r){
                  if (r.success) {
                      $("#response").html("Success!");
                  } else if (r.error) {
                      $("#response").html(r.error);
                  }
              }
          });
      });
  });
  </code></pre>
  
  <p></p>