成功安装Spring Security后如何回调AJAX成功函数?

时间:2021-10-31 19:27:06

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.

我有发出AJAX调用的客户端。这些调用引用服务器端受Spring Security保护的URL。如果用户的会话超时,我在灯箱中有一个登录表单弹出窗口。用户成功登录后,我希望客户端重新执行AJAX调用。

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

以下是进行AJAX调用的客户端代码示例:

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

当身份验证失败时,服务器返回401,导致客户端调用handleError。是否可以将回调函数传递给handleError?我希望回调重新执行

$("#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.

我已经看到了这个问题的解决方案,其中服务器在会话超时的AJAX调用上返回成功响应。然后,success函数在响应中查找某些内容以了解会话超时。然后客户端在那里存储回调函数。我更喜欢在错误功能中处理这个问题。

3 个解决方案

#1


3  

The request executed by

请求执行

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

can be re-executed in the handleError function by calling

可以通过调用在handleError函数中重新执行

$.ajax(this);

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

无需传入回调函数即可重新执行:

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

#2


2  

I've mentioned it here: How to send a form without refreshing the page?

我在这里提到过:如何在不刷新页面的情况下发送表单?

It's the way of having a callback for the 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 could 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);
}

#3


2  

Closures to the rescue:

关闭救援:

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 reference to the error function returned by handleError, the error function has a reference to setupOptions), but this should be easily garbage-collected by any decent browser.

请注意,这会创建一个引用循环(setupOptions具有对handleError返回的错误函数的引用,错误函数具有对setupOptions的引用),但这应该很容易被任何体面的浏览器垃圾收集。

#1


3  

The request executed by

请求执行

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

can be re-executed in the handleError function by calling

可以通过调用在handleError函数中重新执行

$.ajax(this);

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

无需传入回调函数即可重新执行:

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

#2


2  

I've mentioned it here: How to send a form without refreshing the page?

我在这里提到过:如何在不刷新页面的情况下发送表单?

It's the way of having a callback for the 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 could 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);
}

#3


2  

Closures to the rescue:

关闭救援:

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 reference to the error function returned by handleError, the error function has a reference to setupOptions), but this should be easily garbage-collected by any decent browser.

请注意,这会创建一个引用循环(setupOptions具有对handleError返回的错误函数的引用,错误函数具有对setupOptions的引用),但这应该很容易被任何体面的浏览器垃圾收集。