3
votes

I have a web-application in ExtJs. I need to do logout, when response status of Ajax request is 401. I use this code:

Ext.Ajax.on('requestexception', function(connection, response, options){
        if (response){
            if (response.status == 401 && myApp.auth.userInfo != null){
                myApp.auth.logout();
            }
        }
    });

It works, but when I have a few requests myApp.auth.logout(); calls several times and I can't to login again, after reloading page I can login. Is there any filters for requests or some method to make Ext.Ajax.on('requestexception') synchronous, that function myApp.auth.logout(); calls only one time?

2
Did you try to set a buffer config on the listener so that it fires only once within the specified time?Saki

2 Answers

2
votes

Try adding this. This makes sure the success and failure callbacks are not executed, your application should stop (perhaps the following requests hitting your override are due to the failure/callbacks being executed in cascade), then execute logout

Ext.Ajax.on('requestexception', function(connection, response, options){
    if (response){
        if (response.status == 401 && myApp.auth.userInfo != null){
            delete options.failure;
            delete options.callback;
            myApp.auth.logout();
        }
    }
});
1
votes

You can use single: true so your handler will handle just the next firing of the event, and then remove itself.

Ext.Ajax.on('requestexception', function (connection, response, options) {
    response && response.status == 401 && myApp.auth.userInfo != null && myApp.auth.logout();
}, null, {
    single: true
});

(See documentation)