mercredi 22 avril 2015

JavaScript function return value/variable scope confusion [duplicate]

This question already has an answer here:

I've been pulling my hair out over this one. I cannot get the auth variable within the function in the AJAX complete to overwrite the auth variable set inside the checkAuth function.

function checkAuth() {
    var auth;
    $.ajax({
        data: { method: "auth_check" },
        complete: function() {
            if(ajaxResponse) {
                if(ajaxResponse.auth == true) {
                    //user logged in
                    auth = true;
                } else {
                    //user not logged in
                    auth = false;
                }
            }
        }
    });
    return auth;
}

auth is always returned as undefined. How do I set auth to true/false from within the AJAX complete?

Aucun commentaire:

Enregistrer un commentaire