@using (Ajax.BeginForm("Edit", "xyz", new AjaxOptions {
InsertionMode = InsertionMode.Replace,
HttpMethod = "POST",
UpdateTargetId = "divDisplay",
}))
{
<input type="submit" value="Edit" id="Edit" class="formBtn" onclick='return OpenErrorPopup()'/>
}
<script type="text/javascript" language="javascript">
function OpenErrorPopup() {
debugger;
var temp = false;
$.ajax({
cache: false,
url: "/xyz/ChecLogin/",
success: function (UserLoggedOn) {
debugger;
if (UserLoggedOn == "True") {
if (condition) {
temp = false;
return temp;
}
else {
temp = true;
return temp;
}
}
else {
temp = false;
return temp;
}
},
error: function (err) {
temp = false;
return temp;
}
});
debugger;
return temp;
}
</script>
Expected : First call openErrorPopup, execute ajax and then return the value dynamically.
Actual : Though openErrorPopup is called, it returns the temp value directly first to the <input .... onclick=...> and then executes the ajax.
I came to know ajax execution will be asynchronous. So I tried OnBegin event of ajax but thats not returning value. Kindly provide some solution on any of these two issues.
Thanks
if you make the ajax call on the same domain, you can instruct jQuery.ajax to be synchronous. Just pass “async: false” to the ajax options:
and there is no need to return tmp from success and error callbacks, just initialize it.