views:

87

answers:

2

I want to use jQuery's .get method to send an ajax call to the server.

I am using this line:

$.get("InfoRetrieve", { },addContent(data));

As you can see I want to call a function call addContent and pass it the data that is retrieved from the server.

The function addContent is below:

function addContent(data){
    $("#0001").append(data);
}

It doesn't seem to work, can you see why.

+6  A: 

Just change it to:

$.get("InfoRetrieve", { },addContent);

It will take care of passing data when it calls the function.

Doug Neiner
It makes no sense that it works that way but it works!
Ankur
When you tried to use it like this `addContent(data)`, you were actually triggering the function, not passing a reference to it. By omitting the `(data)` you passed a reference to your function that `$.get` could call upon success.
Doug Neiner
I see :) thanks
Ankur
@dcneiner: +1, you got it, I mis-readed the question...
CMS
A: 

Try wrapping it in a new function object:

$.get("InfoRetrieve", { },function() { addContent(data) });
Roy Tang