Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Dec 2009
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Need help with Ajax Timing

    Hi, I'm using js and ajax to do some client-side and server-side form validation. At a point, the client-side function calls the server-side function, but it's not waiting for the return from the server-side function. A boolean variable should get a true or false from the server-side function, but instead it's showing a value of "undefined". I know there's something simple I must be doing wrong, but I haven't been using ajax that long.

    my code looks more or less like this:

    Code:
    function validationForm() {
         ...
         blnPassedBackEnd = doBackEndValidation();
         alert(blnPassedBackEnd + " This alert should fire second, but it is firing first and blnPassedBackEnd has a value of 'undefined'.");
         if (blnPassedBackEnd ) {
              //passed back end validation, do some stuff
         else {
             //did not pass back end validation, do some other stuff
         }
         ...
    }
    
    function doBackEndValidation() {
         var args = "?myVar=whatever";
    
         function objReq() {
    	var objReq = new ActiveXObject("Microsoft.XMLHTTP");
    	objReq.open("POST", "/_ajaxMethods/_somepage.cfm", true);
    	objReq.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    	return objReq;
         }
    
          objReqMsgCnts = new objReq();
          objReqMsgCnts.send(args);
    
          objReqMsgCnts.onreadystatechange = function(){
               if(objReqMsgCnts.readyState == 4){
                    if (objReqMsgCnts.responseText == "0|0") {//no validation failure messages
                         alert("This alert should fire 1st, but it is firing 2nd");
    	        return true;
               }
               else {
               //do some processing to get the error messages
                    return false;
               }
          }
    }
    Thanks much for any help you can provide.

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,329
    Thanks
    23
    Thanked 616 Times in 615 Posts
    Before doing tests on the response you should check that its there.
    Code:
    if (objReqMsgCnts.responseText)
    {
      alert(objReqMsgCnts.responseText);
    }else{
      alert('There was a problem with the request. '+ objReqMsgCnts.status);
    }
    Use this to check your reply then replace the alert with what you want to do.

  • #3
    New Coder
    Join Date
    Dec 2009
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sunfighter View Post
    Before doing tests on the response you should check that its there.
    Code:
    if (objReqMsgCnts.responseText)
    {
      alert(objReqMsgCnts.responseText);
    }else{
      alert('There was a problem with the request. '+ objReqMsgCnts.status);
    }
    Use this to check your reply then replace the alert with what you want to do.
    Hi, thanks for responding. It's a matter of delay -- if the true/false response isn't there yet, I don't want it just to blow past, I want the function to wait until it gets one, and only then continue executing the script. I was thinking of using a while loop -- i.e., while responseText == undefined, do nothing; break out of the loop and continue only when responseText is defined (as true or false). But that could become an infinite loop if there was a problem generating the response on the back end (stored proc on an Oracle server). I figure this can't be an uncommon problem; there must be a simple way to deal with this.

  • #4
    New Coder
    Join Date
    Dec 2009
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thumbs up Figured It Out

    I figured out what has to happen - and it is simple. It's the 3rd argument in the open() function. This has to be set to false instead of true, and this makes the request synchronous. So:

    Code:
    function objReq() {
    	var objReq = new ActiveXObject("Microsoft.XMLHTTP");
    	objReq.open("POST", "/_ajaxMethods/_somepage.cfm", false);
    	objReq.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    	return objReq;
         }
    Thanks!

  • #5
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    You should never ever never ever use synchronous XMLHttpRequests. If you think you are required to do that to reach your goal ... the goal is clearly defined incorrectly :-)

    Synchronous XMLHttpRequests will freeze your browser for as long as the request is not finished. This is exactly the opposite to a good user experience.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •