Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2007-10-11
7
Medium Priority
?
2,166 Views
Last Modified: 2008-01-09
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!
0
Comment
Question by:td234
  • 4
  • 3
7 Comments
 
LVL 23

Accepted Solution

by:
basicinstinct earned 2000 total points
ID: 20061601
Asynchronous puts the 'A' in AJAX - that's why it's not waiting.

in your ajax request add this:

asynchronous: false,

           new Ajax.Request("/ajax/login.php", {
                  method: 'post',
                  asynchronous: false,
                  ...

Further reading here under "The asynchronous Property":
http://www.wrox.com/WileyCDA/Section/id-306214.html
0
 
LVL 2

Author Comment

by:td234
ID: 20061798
I tried your suggestion in my sample above and it did not work. I still get the HERE alert before anything else.
0
 
LVL 23

Expert Comment

by:basicinstinct
ID: 20061942
have a look at this:

"The asynchronous Property":
http://www.wrox.com/WileyCDA/Section/id-306214.html
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:td234
ID: 20067349
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.


0
 
LVL 23

Expert Comment

by:basicinstinct
ID: 20070046
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...
0
 
LVL 2

Author Comment

by:td234
ID: 20070244
I am running this off the server.
0
 
LVL 23

Expert Comment

by:basicinstinct
ID: 20071839
what about using onSuccess: or onComplete: instead of afterFinish:
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

580 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