I have a function:
var foo=function(){
var ret="false";
var data={};
data["blah"]="blah";
$.ajax({
type: "POST",
url: "https://website.com/a",
data: data,
dataType: "json"
}).success(function (data) {
ret=data["stuff"];
alert("set to "+ret);
}).error(function (a, b, c) {
alert("error");
ret="false";
});
return ret;
}
when I do the following:
alert(foo());
I get the follow order of output:
1.false
2.set to true
I was expecting to get ret set to true and then returning false, but this is not what’s happening. What am I doing wrong?
The
$.ajaxis async by default. So basicly youreturn ret;earlier then javascript set it to ajax response.Try to use
async: falsein$.ajaxcall options.