1
function saveCallerReference(callerReference){
    $.getJSON('/index.php?r=site/AJAXsaveCallerReference', function(data) {
        console.log(data);
        return data;
    });

}

Given the above, the line "return data;" never gets returned, when the function(data){} exits, where does that return go? I want my outer scope function, saveCallerReference, to return the value from the getJSON(). console.log() is printing correctly so I am getting the data.

user971994
  • 13
  • 2

1 Answers1

4

Where do you expect it to be returned to? the function being called is anonymous...

You need to understand that $.getJSON happens asyncronously so the normal top-down flow does not apply, you need to do whatever you need to trigger whatever you want to do with data inside the callback...

$.getJSON('/index.php?r=site/AJAXsaveCallerReference', function(data) {
    functionThatDoesWhatYouWant(data);
});
jondavidjohn
  • 61,812
  • 21
  • 118
  • 158