Solved

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

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
using CK editor in iframes 5 28
TFS 2015 Access denied. 1 30
Error building VS2105 solution from repository 1 40
PowerShell: Adding ToGB to a script 4 30
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

730 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