DanielMedia wrote:
This function always returns false. The success variable is not being
changed within the getJSON function. I'd appreciate any help. Thanks.

var check_user = function()     {
        var success = false;
        $.getJSON(url, function(response)       {
                if(response.ok) {
                        alert(response.ok); // (evaluates to true)
                        success = true; // The parent "success" variable 
doesn't change.
                }
        });
        return success;
}

The trouble is that the 'A' in 'AJAX' stands for 'Asynchronous'. The .getJSON call returns immediately, before the anonymous callback completes. You will need to structure somewhat differently if you want to use the results of the AJAX call in this manner. I would suggest passing in a callback function to your check_user function

    var check_user = function(handler) {
        $.getJSON(url, function(response) {
        if (response.ok) {
            handler(true);
        } else {
            handler(false);
        }
    }

    // ...

    check_user(function(userOk) {
        alert("User " + (userOk ? "" : " not") + " ok");
        // ... whatever you need to do here.
    });

Good luck,

  -- Scott

Reply via email to