0

I called ajax (which return json data) from a javascript function like below

function getDataSets(groupID)
{
    $.ajax({
        type: 'POST',
  dataType: "json",
        url: 'del/deljson.php',
  data:'groupID='+groupID,
        success:function(msg){
   //alert(msg);
   return msg; 
        }
    });
}

when i call this function as alert(getDataSets(1)); it return "undefined". However if i call alert on success of ajax it shows return data. what I am doing wrong.

  • The return inside the `success` is only for that function. Try to take a variable inside the function (outside of the Ajax call) and then set the variable inside the ajax call. And return it at the end of the function. – Exception_al Feb 23 '18 at 08:14
  • i did this: function getDataSets(groupID) { var json=""; $.ajax({ type: 'POST', dataType: "json", url: 'del/deljson.php', data:'groupID='+groupID, success:function(msg){ json=msg; } }); return json; } but still no success – user3308894 Feb 23 '18 at 08:25
  • Check this link as suggested : https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Exception_al Feb 23 '18 at 08:27

0 Answers0