Ich bin nicht sehr erfahren mit JavaScript und ich habe Probleme mit Variablen Umfang und jquery. Ich habe 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"
}
Fehle ich da etwas? Ich denke, valid
sollte eine globale Variable sein, und daher sollte Option 2 gut funktionieren. Ich bin wirklich nicht sicher über die Option 1.
Kann mir jemand einen Ratschlag geben, wie man das am besten funktioniert?
Vielen Dank.
Eww, weniger globale Variablen wie möglich. In solchen Fällen machst du etwas falsch. – Dykam