javascript - jQuery AJAX callback does not set variable before function returns? -



javascript - jQuery AJAX callback does not set variable before function returns? -

i have 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 "+ret); }).error(function (a, b, c) { alert("error"); ret="false"; }); homecoming ret; }

when following: alert(foo());

i follow order of output:

1.false

2.set true

i expecting ret set true , returning false, not what's happening. doing wrong?

the $.ajax async default. basicly return ret; before javascript set ajax response.

try utilize async: false in $.ajax phone call options.

javascript jquery

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -