Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I callback AJAX success function after successful Spring Security login?

I have clients that issue AJAX calls. These calls reference URLs that are protected by Spring Security on the sever side. If the user's session has timed out, I have a login form popup in a lightbox. After the user has successfully logged in, I would like the client to re-execute AJAX call.

Here's an example of the client side code that makes an AJAX call:

function handleSearchClick(evt) {
    var setupOptions = { 
        success: loadSearch,
        type: "POST",
        dataType: "json",            
        url:   "../search.ajax",
        error: handleError, // how can I pass callback info i.e. I want to be able to execute $("#searchForm").ajaxSubmit(setupOptions); from handleError? 
        timeout: 50000
    };                
    $("#searchForm").ajaxSubmit(setupOptions);
}

When the authentication fails, the server returns a 401 which results in the client calling handleError. Is it possible to pass a callback function to handleError? I would want the callback to re-execute

$("#searchForm").ajaxSubmit(setupOptions);

I have seen solutions to this problem where the server returns a success response on AJAX calls that have a session timed out. Then, the success function looks for something in the response to know the session timeout. The client then stores a callback function there. I prefer though to handle this in the error function.

like image 552
James Avatar asked Jan 07 '13 15:01

James


People also ask

What triggers Ajax success?

AJAX success is a global event. Global events are triggered on the document to call any handlers who may be listening. The ajaxSuccess event is only called if the request is successful. It is essentially a type function that's called when a request proceeds.

How do I know if Ajax request is successful?

You can check when exactly it returns "success" : // If successful, handle type chaining if ( status >= 200 && status < 300 || status === 304 ) { ... // If not modified if ( status === 304 ) { statusText = "notmodified"; ... // If we have data } else { try { ...

How do you call a controller in a success with Ajax?

So, add a success option to your partial-view ajax request and then get the response and render it in the div you want the partial-view to be visible as: success: function(response){if(response!= null) $('<yourDivInWhich you want to render the partial view>'). html(response)} .

What is difference between success and complete in Ajax?

success() only gets called if your webserver responds with a 200 OK HTTP header - basically when everything is fine. However, . complete() will always get called no matter if the ajax call was successful or not - maybe it outputted errors and returned an error - . complete() will still get called.


1 Answers

The request executed by

$("#searchForm").ajaxSubmit(setupOptions);

can be re-executed in the handleError function by calling

$.ajax(this);

There is no need to pass in a callback function to re-execute:

$("#searchForm").ajaxSubmit(setupOptions);
like image 136
James Avatar answered Sep 19 '22 14:09

James