I have a quick question on JavaScript:
I have the following line of code shows an example and it works fine. You can access passeddata there are no issues.
$.getJSON(jsonUrl,function(passeddata){
alert("it worked ");
});
The next code sample does not work and fails with the following error:
Uncaught TypeError: Object ReferenceError: passeddata is not defined has no method 'replace' jq.html:177 (anonymous function)
$.getJSON(jsonUrl, something(passeddata));
function something(passeddata)
{
var jasondata = passeddata;
alert("it worked ");
}
Can someone explain the issue? I know its probably something obvious, but I'm just not able to find an answer.
In the first case, you are passing a function to getJSON
that gets executed when the HTTP request for the JSON comes back.
In the second, you are calling the function immediately and passing its return value to getJSON
.
Don't call it yourself, take the ()
away: $.getJSON(jsonUrl, something);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With