Solved

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

Posted on 2008-10-30
3
1,476 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
  • 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now