-2

I'm cannot return data from $.post to function() pls help

refer url : Data from post request

test = {
    
  getData : function (callback){
    
    $.post("getdata.php",{data: data},function(data){
      
          callback(data);
      }
           
    }

}
           
var data = test.getData(callback);     
           
console.log(data);
    

    

** TypeError : callback is not a function

thank you

Community
  • 1
  • 1

1 Answers1

0

The callback like you think is not a keyword or default argument. It is the reference of the function to be executed after the AJAX call is performed. You haven't declared what's callback. You have to do something like:

function callback (data) {
  // do something with data
}

You can also define callback this way:

var callback = function (data) {
  // do something with data
}
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252