dimanche 28 juin 2015

Cannot redirect inside the custom success callback on an Ajax wrapper

I am trying redirect a user to the login page if he/she is not logged in anymore or do not have the permission when doing certain functionalities via Ajax. To not get redundant, I implemented a wrapper for the success callback based on the code that I found here that checks if the controller returned not_logged_in to signify that the user does not have permission. If the user doesn't have permission, I redirect the user using the custom success function. The problem is that it doesn't redirect the user to the login page. Instead, it just prints the login page on the console and the actual Ajax call fails. What should I change in my code?

Here is the Ajax wrapper:

function wrapped_ajax(options) {
    var success = options.success;
    options.success = function(data, textStatus, jqXHR) {
        if(success) {
            if (data.not_logged_in) {
                window.location = 'login';
            } else {
                success(data, textStatus, jqXHR);
            }
        }

    };
    return $.ajax(options);
}

Aucun commentaire:

Enregistrer un commentaire