1

I am using a php to form a json object from sql and using json_encode($myarr) which is working perfectly. in my javascript i am using a util function to get the json which is also working fine.

var get_schedule_service = webservices.get_schedule + "/" + 0;
generic_get(get_schedule_service, function(response) {
    if (response) {
        console.dir(response);
    } else {
        alert('empty schedule');
    }
});

the above code outputs the proper json.

The problem i am facing is when i try to store the output in a javascript var i get an error that the var is undefined.

i have also tried putting the entire webserice in an array something like this:

var get_schedule_service = webservices.get_schedule + "/" + 0;
var my_json_array = generic_get(get_schedule_service, function(response) {
    if (response) {
        return response;
    }
});
Krupesh Kotecha
  • 2,396
  • 3
  • 21
  • 40
Maxxer
  • 91
  • 8

1 Answers1

0

Since AJAX request is async by nature, you need to pass a callback like this, which can be called when the response is received:

var get_schedule_service = webservices.get_schedule + "/" + 0;
generic_get(get_schedule_service, function (response, myCallback) {
                if (response) {
                   myCallback(response);
                }
                });


function myCallback(response){
   console.log(response);
}
Sandeep Nayak
  • 4,649
  • 1
  • 22
  • 33