Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$.getJSON function passing parameter

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.

like image 846
DevilCode Avatar asked Dec 05 '22 13:12

DevilCode


1 Answers

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);

like image 194
Quentin Avatar answered Dec 28 '22 11:12

Quentin