Solved

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

Posted on 2008-10-30
3
1,440 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
Comment Utility
>>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
Comment Utility
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
Comment Utility
>>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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

9 Experts available now in Live!

Get 1:1 Help Now