I have a button named searchReportButton. On this button click, I need to check session value from server using a ajax web method “Error.aspx/CheckSessionExpiry”.This is done in checkSession javascript function. The checkSession is not returning anything now – instead it is handling the required operation based on the result (redirecting to error page).
- I need to return the result of ajax web method to the Main Caller. How can we do this return?
- Do I need to move the remaining code (from Main Caller) into sucsess callback?
Main Caller
searchReportButton.click(function () {
checkSession();
//Remainging Code
});
Helper
function checkSession() {
var win = 101;
$.ajax(
{
type: "POST",
url: "Error.aspx/CheckSessionExpiry",
data: '{"winNumber": "' + win + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: handleSessionResult
}
);
}
Result Helper
function handleSessionResult(result) {
if (result.hasOwnProperty("d"))
{
result = result.d
}
if (result == "ACTIVE")
{
window.location.href("Error.aspx");
return false;
}
//alert(result);
}
REFERENCE:
You cannot. Or, to be more specific, you should not. Ajax request is
asyncwhich means it does not block other JS operations while it’s being proceeded (imagine 5s browser freeze if the server was responding slowly?).Instead you can pass a
successcallback, which you already do. Then do anything you wanted in there.So instead of:
you should move the remainging code inside the callback: