How can I execute an AJAX function after a successful Spring security login?

I have clients that call AJAX calls. These calls refer to URLs protected by Spring Security on the server side. If the user session has ended, I have a pop-up window of the lightbox login form. After successfully logging in, I want the client to re-execute the AJAX call.

Here is a sample client-side code that calls 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 ie I want to be able to execute $("#searchForm").ajaxSubmit(setupOptions); from handleError? timeout: 50000 }; $("#searchForm").ajaxSubmit(setupOptions); } 

When authentication fails, the server returns 401, with the result that the client raises a handleError. Is it possible to pass a handleError callback function? I would like the callback to re-execute

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

I have seen solutions to this problem when the server returns a success response to AJAX calls that have a session timeout. The success function then searches for something in the response to find out the session timeout. The client then saves the callback function. I prefer to handle this in error function.

+7
source share
3 answers

Request Fulfilled

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

can be re-executed in the handleError function by calling

 $.ajax(this); 

There is no need to pass a callback function to execute again:

 $("#searchForm").ajaxSubmit(setupOptions); 
+3
source

I mentioned it here: How to submit a form without refreshing the page?

This is a way to have a callback for an error handler.

 $.ajax({ url: siteUrl + 'fetch/search', type: "POST", data: formData, success: function(data) { ..... }, error:function(x,e){ if(x.status==0){ alert('You are offline!!\n Please Check Your Network.'); }else if(x.status==404){ alert('Requested URL not found.'); }else if(x.status==500){ alert('Internel Server Error.'); }else if(e=='parsererror'){ alert('Error.\nParsing JSON Request failed.'); }else if(e=='timeout'){ alert('Request Time out.'); }else { alert('Unknow Error.\n'+x.responseText); } } }); 

But you can do the following

 function handleSearchClick(evt) { var setupOptions = { success: loadSearch, type: "POST", dataType: "json", url: "../search.ajax", error: handleError(x,e), // this should do it timeout: 50000 }; $("#searchForm").ajaxSubmit(setupOptions); } 
+2
source

Closures for salvation:

 function handleSearchClick(evt) { var setupOptions = { success: loadSearch, type: "POST", dataType: "json", url: "../search.ajax", timeout: 50000 }; setupOptions.error = handleError(setupOptions); $("#searchForm").ajaxSubmit(setupOptions); } function handleError(setupOptions) { return function () { // handle error // then re-submit $("#searchForm").ajaxSubmit(setupOptions); }; } 

Note that this creates a reference loop (setupOptions has a link to the error function returned by handleError, the error function has a link to setupOptions), but this should be easily garbage collected by any decent browser.

+2
source

All Articles