Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExtJs Ajax request exception synchronous

Tags:

extjs

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?

like image 824
Oxidizer Avatar asked Feb 15 '26 23:02

Oxidizer


2 Answers

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();
        }
    }
});
like image 176
Francis Ducharme Avatar answered Feb 18 '26 11:02

Francis Ducharme


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)

like image 31
CD.. Avatar answered Feb 18 '26 13:02

CD..