I am using Grails 1.3.7 and the latest spring-security-core plugin. The following method has been implemented in my LoginController:
def authAjax = { response.setHeader 'Location', SpringSecurityUtils.securityConfig.auth.ajaxLoginFormUrl response.sendError HttpServletResponse.SC_UNAUTHORIZED }
And in my global JavaScript file, I have the following:
$.ajaxSetup({ error: function(xhr, status, err) { if (xhr.status == 401) {
The login form is a standard login form directly from the plugin documentation. The only difference is that I submit my form using jQuery as follows:
var params = $('#ajaxLoginForm').serialize(); $.post($('#ajaxLoginForm').attr('action'), params, function(jsonData) { if (jsonData.success) { $('#login-dialog').dialog('close'); } else { alert('TODO: display errors'); } }, 'json');
The problem is that the first time I click on the login button, I seem to authenticate normally, but the response returned from the server is a 302 redirect based on the Referer request header. Thus, the body of my $ .post () never starts. I get HTML instead of JSON. In fact, this does not affect my LoginController.ajaxSuccess method until the second view. I read and re-read the documentation, and something is missing.
UPDATE: It looks like this might not be the Referer problem, since the second time the form is submitted, the Referer still exists. Therefore, I completely lose why I have to submit the form twice for the ajaxSuccess method to call.
Gregg source share