javascript - return value after callback function Not Working -
this question has answer here:
- how return response asynchronous call? 21 answers
i don't know below valid question? or stupidity.
function isslaexists(department) { var flag = ""; $.ajax({ type: "post", data: "type=isslaexists&department=" + encodeuri(escape(department)), url: "class-accessor.php", success: function (data) { //flag=data; flag = "yes"; } }); return flag; } alert(isslaexists('department'));
i'm trying return value of flag
function returns blanks if set value of flag maually. i'm doing wrong?
$.ajax({ type: "post", data: "type=isslaexists&department=" + encodeuri(escape(department)), url: "class-accessor.php" }).done(function(r){ alert("flag"); });
this give alert when ajax request successfull. r
contain data response got you. can now, example, call function inside done function process request.
since $.ajax returns promise, return ajax call , chain functions together
function ajaxcall(department) { return $.ajax({ type: "post", data: "type=isslaexists&department=" + encodeuri(escape(department)), url: "class-accessor.php" }); } ajaxcall("mydept").done(function(response){ alert(response); })
Comments
Post a Comment