0

I followed the debugger and I am receiving data from my server but the objData variable is always undefined when pass the data from the GetAjax function to the GetPlacesAroundMe.

As you can see I set the ajax to be sync.

*The code is added below

Thanks, I will appreciate any help.

Model.prototype.AjaxGet = function(sUrl,sData,sType){
     var result="";
    $.ajax({
           url: sUrl+"?"+sData,
           type: sType,
           async: false,
           success: function(data){

              return (data);

           },
           error:function(exception){
           alert(exception.responseText);
           }
    });
    //return result;
}

 Model.prototype.GetPlacesAroundMe = function(data){


    var objData = this.AjaxGet(ip_address+"/purpleserver/index.php/selectController/GetPlacesAroundMe",data,"get");

     return objData;

}
James Montagne
  • 77,516
  • 14
  • 110
  • 130
Roy
  • 137
  • 2
  • 7
  • 1
    because $.ajax is asynchron – Bernhard Oct 04 '13 at 17:14
  • pass another parameter called callback and return it in your success/error functions – Kishore Oct 04 '13 at 17:17
  • kishore- I tried it and it didn't work. And i set sync to false, meaning it is async, am I right? – Roy Oct 04 '13 at 17:25
  • @Kishore you can't return anything from the callback. that isn't supported. – Kevin B Oct 04 '13 at 17:29
  • James- I don't think, it is a duplicate, the different is that I have 3 functions: foo()- who calls GetPlacesAroundMe()- which calls AjaxGet. data received in the foo function is undefined- this is my problem. – Roy Oct 04 '13 at 17:31
  • kishore- is their any workaround? which i can use? – Roy Oct 04 '13 at 17:32
  • @Roy Working with async functions, you have two options: callbacks or jquery `deferred` objects. Adding more functions doesn't change that, just the complexity. You could pass a callback from the front all the way to `AjaxGet` or pass a `promise` from `AjaxGet` all the way to the front. It's up to you. – Jason P Oct 04 '13 at 17:43
  • @KevinB he can pass another argument in the AjaxGet function called callback and in the success function Roy can just do this right callback(data); why wont this work? – Kishore Oct 04 '13 at 17:51
  • That will work just fine. I was referring to this: *"pass another parameter called callback and **return** it in your success/error functions"* you can't return it, but you can execute it. – Kevin B Oct 04 '13 at 17:52
  • I can do the trick of passing callback, and it's work but I can't return it. :( – Roy Oct 05 '13 at 08:27

0 Answers0