Link to home
Start Free TrialLog in
Avatar of dany651
dany651Flag for Canada

asked on

Ajax, how wait for the response of the PageMethods call result ?

Hi, make a call to a function in my code behind (C#) from javascript with Ajax

My function 'Test' call my Function 'Toto' and then after i have no control,  my 'Test' function continue running,  so my var MyResult is always null in my 'Test' method.

I would like to wait for the result of OnSucceedd to continue running my 'Test' function, so after the call to 'Toto' i have my var 'MyResult' set by the result of my OnSucceedd.


Here is my code  :


var MyResult  = null;

function Test()
{
      Toto();

      if(MyResult == true)
            blablabla...
}

Toto()
{
     PageMethods.FindRequesterNameMatch(1, 2, onSucceeded, onFailed);
}


function onSucceeded(result, userContext, methodName)
{
        MatchExist = result;
}

function onFailed(error, userContext, methodName)
{
        MatchExist = false;
}
Avatar of hielo
hielo
Flag of Wallis and Futuna image

>>my 'Test' function continue running,  so my var MyResult is always null in my 'Test' method
Correct, but once Toto has completed, it should call onSucceeded. So the if should really be in onSucceeded (although it's not clear where you are setting the value of MyResult):
function onSucceeded(result, userContext, methodName)
{
        MatchExist = result;
 
if(MyResult == true)
            blablabla...
 
}

Open in new window

Avatar of dany651

ASKER

I know but my Test function are called by a button ( OnClick="return Test();"  ) so i want to verify the result before continue.....

TEST ==

result = called my webMethod via Ajax....

IF  MyResult == true
           return true to button and execute ButtonClick() server side code
ELSE
            return false to button and stop process              
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial