0

I have the following function but I can't seem to get returnedData = result to successfully set the value. If I include alert(result) in the same location, the popup displays the string I'm looking for correctly.

Is there something I'm missing here? I had thought because I declared the variable returnedData outside the function it would be accessible everywhere?

function AJAXprocesstwoVariables(Var1, Var2) {
    var V1 = Var1,
        V2 = Var2;
        var returnedData;
    $.post(
        processinglocation, { 
            data1: V1,
            data2: V2 
        },
        function (result) {
            returnedData = result; // *<- this doesn't work*
            // alert(result); // *<-this works*
        }
    );
    return returnedData;
}

var ReturnedInfo = AJAXprocesstwoVariables(Var1, Var2);
$('body').append(ReturnedInfo);
denski
  • 1,768
  • 4
  • 17
  • 35

1 Answers1

0

$.post will run asynchronously. Once that line is run, the request will be made in the background, and you will immediately hit the return line of AJAXprocesstwoVariables. You are attempting to use returnedData before it is set in the success function of your $.post.

Gavin
  • 4,365
  • 1
  • 18
  • 27