-1

I know there is a topic that covers this (How do I return the response from an asynchronous call?) but I do not understand it...

What i'm trying todo is get a specific value for each variable i specify from my getInfo function:

var colour = getInfo(schedule.schedule_id, 'colour');
var date = getInfo(schedule.schedule_id, 'date');

function getInfo(scheduleid, action) {

    $.ajax({
        url: 'ajax.php?schedule_id='+scheduleid,
        type: 'GET',
        dataType: 'JSON',
        cache: false,
        success: function(data){
            successCallback(data, action);
        },
        async: false
    });
}

function successCallback(data, action){
    return data.action;
   }

i get undefined....

EDIT How can i return an array from the ajax data so I can use it like var[1], or var[2]..etc ??

Community
  • 1
  • 1
rubberchicken
  • 1,270
  • 2
  • 21
  • 47

2 Answers2

1
var colour, date;

function getInfo(scheduleid) {

    $.ajax({
        url: 'ajax.php?schedule_id='+scheduleid,
        type: 'GET',
        dataType: 'JSON',
        success: function(data){
            colour = data.colour;
            date = data.date;
        }
    });
}

And your PHP file needs to return something like this:

{ colour: 'red', date: '10-31-2014' }
Sherman
  • 853
  • 5
  • 16
0

Try return data[action] instead of return data.action

Terje Bråten
  • 264
  • 2
  • 6