Solved

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

Posted on 2008-10-30
3
1,554 Views
Last Modified: 2013-12-17
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;
}
0
Comment
Question by:dany651
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 22847158
>>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

0
 

Author Comment

by:dany651
ID: 22870792
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              
0
 
LVL 82

Accepted Solution

by:
hielo earned 250 total points
ID: 22947278
>>my Test function are called by a button ( OnClick="return Test();"  ) so i want to verify the result before continue
Before you continue with what? I suspect you meant "Before you continue to submit the form". If that is the case you do not need to do:
OnClick="return Test();"

instead you do:
OnClick="Test(); return false;"    

so that the submit button NEVER submits the form upon click. Instead you will "wait" for the call to completed and then if your condition is met, then you programatically submit the form.
function onSucceeded(result, userContext, methodName)
{
        MatchExist = result;
 
  if(MyResult == true){
            //blablabla...
   //assuming your html has <form name="myForm"...>
   document.forms.myForm.submit();
  } 
}

Open in new window

0

Featured Post

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article discusses how to implement server side field validation and display customized error messages to the client.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question