I am trying to pass a function to another function to use in its call back, I call the function using…
call("login", {email:user,password:pw}, alert(getResponse()));
call is defined as:
function call(url, p, f){
$.getJSON(baseUrl + url, p, f(data));
};
and getResponse is defined as:
function getResponse(d){
return d.result.success;
};
data is returned by getJSON (see here: http://api.jquery.com/jQuery.getJSON/)
How am I meant to pass data to that function?
The function is called, because it hits a break point I set.
When getResponse is called though I get Uncaught ReferenceError: data is not defined
Use
The expression
alert(getResponse())is the call of the alert function (withgetResponsecalled without any parameter, and thus this would throw an error asd.resultwould mean takingresultfromundefined), butfunction(data){alert(getResponse(data)})is the definition of a new function taking data as parameter.Also on a note to that you do not need to have
f(data)in$.getJSON(baseUrl + url, p, f(data));you only requiref, so you would call$.getJSON(baseUrl + url, p, f);