Link to home
Start Free TrialLog in
Avatar of td234
td234

asked on

JS not waiting for prototype Ajax.Request to complete before proceeding

I am using prototype.js for this process.

I have a script that check a login form and uses JS to go check the form be for submitting. Code below. Problem is the script id not waiting for the result of the Ajax before proceeding so JS returns true before the Ajax request is even finished. In my test code below I get the alert with "HERE" before I get the result form the Ajax. How can I set my tell my script to wait for the result from Ajax.Request?


function check_login(me) {
      var email = $F('loginEmail');
      var password = $F('loginPasswd');
      if(email=='') {
            alert('You must enter a your email address to sign up.');
            return false;
      } else if(password=='') {
            alert('You must enter a your password to sign up.');
            return false;
      } else {
            new Ajax.Request("/ajax/login.php", {
                  method: 'post',
                  parameters: { email: email, passwd: password },
                  afterFinish: function(transport) {
                        var data = transport.responseText;
                        if(data==0) {
                              alert('zero');
                              $('loginError').update('Your email and password are not in our system.');
                              new Effect.Shake($('loginForm'));
                              return false;
                        } else {
                              alert('passed?');
                              return false;                        
                        }
                  }
            });
            alert("HERE");
      }
}


Thanks!
ASKER CERTIFIED SOLUTION
Avatar of basicinstinct
basicinstinct
Flag of Australia 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
Avatar of td234
td234

ASKER

I tried your suggestion in my sample above and it did not work. I still get the HERE alert before anything else.
have a look at this:

"The asynchronous Property":
http://www.wrox.com/WileyCDA/Section/id-306214.html
Avatar of td234

ASKER

Dude, I hear you. I agree that sure sounds like the solution. But it does not work. I added it to the JS and it still submits the form before one of the test alerts are called. I get the test alert HERE immediately, so the script is getting past the "new Ajax.Request" long before it is processed.


one thing that often makes ajax behave strangely is if you run your test page under the 'file:' protocol, ie if you are running it off your local filesystem rather than a webserver...

if you are running off filesystem then try again on a webserver...
Avatar of td234

ASKER

I am running this off the server.
what about using onSuccess: or onComplete: instead of afterFinish: