Jquery $.post() variablenspeicher

Ich bin nicht Massiv Erfahrung mit JavaScript, und ich habe Probleme mit dem Geltungsbereich von Variablen und jquery. Ich habe die folgende Struktur:

function pass_variables()
{
    username = "efcjoe"
    response = post_variables(username)
    alert(response)
}

function post_variables(username)
{
    $.post(
        '/path/to/url/',
        {
            'username': username,
        },
        function(data)
        {
            valid = (data != 0) ? true : false

            //OPTION 1: If I put return here...
            return valid; //... the alert box in pass_variables says "undefined"
        },
        "text"
    );

    //OPTION 2: If I put return here...
    return valid; //... The alert box does not pop up, and Safari debug gives
                  //    me the error: "Can't find variable: valid"
}

Übersehe ich da etwas? Ich denke valid sollte eine Globale variable, und daher option 2 sollte gut funktionieren. Ich bin wirklich nicht sicher über option 1.

Kann jemand mir einen Rat geben auf der beste Weg, um dies zu bewerkstelligen?

Vielen Dank.

Eww, weniger Globale vars wie möglich. In Fällen wie diesem, dann machst du etwas falsch.

InformationsquelleAutor user116170 | 2009-08-09

Schreibe einen Kommentar