views:

74

answers:

3

instead of writing code in the standard way:

$.get('test.xml',function(){
    //manipulate the code here
})

I wanted to write the code this way to make things easier:

$.get('test.xml',callback(data));

function callback(data){
    //manipulate with the data below...
}

but error show "data is undefined", how can i fix this?

+5  A: 

Just write

$.get('test.xml',callback);

When you write

$.get('test.xml',callback(data));

then callback gets executed immediately (you call the function).


Or if data is not supposed to be the data returned from the Ajax call, but some parameter you want to pass to the function, you have two possibilities:

  1. Make callback return a function.
  2. Wrap the callback call in an anonymous function:

    $.get('test.xml',function(){callback(data);});
    
Felix Kling
+2  A: 

Just leave the data out. it is a parameter automatically given to the callback function:

$.get('test.xml',callback);

function callback(data){
    //manipulate with the data below...
}
jigfox
A: 

try:

$.get('test.xml',function(){callback(data)});

JS can't handle variables for callback-functions your way

helle