-1

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).

  1. I need to return the result of ajax web method to the Main Caller. How can we do this return?
  2. 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:

  1. How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?
  2. $.ajax context option
  3. jQuery Ajax Callback does not get Context
  4. jQuery pass more parameters into callback
Community
  • 1
  • 1
LCJ
  • 22,196
  • 67
  • 260
  • 418
  • 2
    possible duplicate of [Variable doesn't get returned JQuery](http://stackoverflow.com/questions/12475269/variable-doesnt-get-returned-jquery) – Denys Séguret Dec 02 '12 at 10:42
  • Not a duplicate. This question has an extra part -- How to deal the `remaining code` part. It is answered also. – LCJ Feb 12 '13 at 07:11

1 Answers1

2

You cannot. Or, to be more specific, you should not. Ajax request is async which 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 success callback, which you already do. Then do anything you wanted in there.

So instead of:

checkSession();
//Remainging Code

you should move the remainging code inside the callback:

function handleSessionResult(result) {
    //... current code
    //Remaining code from the above
}
Michał Miszczyszyn
  • 11,835
  • 2
  • 35
  • 53
  • Will the remaining code in Main Caller be executed before it completes the ajax call? – LCJ Dec 02 '12 at 10:49
  • Yes, because ajax is asynchronous. – Michał Miszczyszyn Dec 02 '12 at 10:51
  • Can sync:true in Ajax method help? – LCJ Dec 02 '12 at 10:51
  • 2
    @Lijo, yes, but you should never use it. If you set async to false you are no longer doing AJAX - you are freezing the client browser. You are basically killing the whole purpose of AJAX which is meant to be asynchronous. – Darin Dimitrov Dec 02 '12 at 10:54
  • No, ajaxes are supposed to be async. If they weren't user's browser would freeze for as long as it took to execute the ajax. – Michał Miszczyszyn Dec 02 '12 at 10:54
  • @DarinDimitrov If `async = false` is not good, how can I ensure that remaining code will not execute before the ajax call is complete? Do you think what I am doing at present is the best possible solution? – LCJ Dec 02 '12 at 10:57
  • 2
    @Lijo, very easy - you should put the remaining code inside the success callback -> there shouldn't be any code after the AJAX call that relies on the result of this call. – Darin Dimitrov Dec 02 '12 at 10:58