Synchronous AJAX needed

I found the following PAQ:

http://www.experts-exchange.com/Web/Web_Languages/XML/Q_21434916.html

but I don't see the working example which was requested.  I've already researched this at several sites, but none of the examples work for me.  Let me share the javascript function I already have:

function makeSyncRequest(data, field){
      var originalField = field;
      var fullURL = document.URL;
      var loyaltyLocation = fullURL.indexOf("/loyalty/") + 9;
      var url = fullURL.substr(0,loyaltyLocation) + "ajax?";
      http_request = false;

      if (window.XMLHttpRequest) { // Mozilla, Safari,...
                  http_request = new XMLHttpRequest();
                  if (http_request.overrideMimeType) {
                        http_request.overrideMimeType('text/xml');
                        // See note below about this line
                  }
            } else if (window.ActiveXObject) { // IE
                  try {
                        http_request = new ActiveXObject("Msxml2.XMLHTTP");
                  } catch (e) {
                        try {
                              http_request = new ActiveXObject("Microsoft.XMLHTTP");
                        } catch (e) {}
                  }
            }
            if (!http_request) {
                  alert('Giving up :( Cannot create an XMLHTTP instance');
                  return false;
      }
      http_request.async = false;
      http_request.open('GET', url + data, false);
      http_request.onreadystatechange = function () {
      if (http_request.readyState == 4) {
            if(http_request.status == 200)
                        alert("came back!");
                  var xmldoc = http_request.responseXML;
                  var ajaxValue = xmldoc.getElementsByTagName("Root")[0].childNodes[0].nodeValue;
                  if (ajaxValue == "0") {
                        alert("DCL " + validatingDCL + " is invalid.");      
                        setTimeout("originalField.select()", 5);      
                  }
           else
            alert('There was a problem with the DCL Validator request.');
        }
      };
      alert("after onreadystatechange");

      http_request.send(null);
      
}

Bottom line I need to see "came back!" or "There was a problem with the DCL Validator request."  BEFORE "after onreadystatechange".  I want a Synchronous implementation of AJAX.  Can you please provide suggested modifications to the above code so that I get into the "came back!" code?  Thanks a ton!
LVL 3
ggridleyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Andy_NeodyniumCommented:
Are you missing a couple of brackets? Should the bottom if statements look like they should be:

if (http_request.readyState == 4) {
    if(http_request.status == 200) {  // << Change here
        alert("came back!");
        var xmldoc = http_request.responseXML;
        var ajaxValue = xmldoc.getElementsByTagName("Root")[0].childNodes[0].nodeValue;
        if (ajaxValue == "0") {
            alert("DCL " + validatingDCL + " is invalid.");    
            setTimeout("originalField.select()", 5);    
        } else { // << Changes here
            alert('There was a problem with the DCL Validator request.');
        } // << Added one here.
    }
};

based on the indentation. Just at the moment, your code will get responseXML irrespective of the request's HTTP status - it's only controlling the alert...
0
Andy_NeodyniumCommented:
Oh, sorry, got that wrong, should be:

if (http_request.readyState == 4) {
    if(http_request.status == 200) {  // << Change here
        alert("came back!");
        var xmldoc = http_request.responseXML;
        var ajaxValue = xmldoc.getElementsByTagName("Root")[0].childNodes[0].nodeValue;
        if (ajaxValue == "0") {
            alert("DCL " + validatingDCL + " is invalid.");    
            setTimeout("originalField.select()", 5);    
        } // << Change here
    } else { // << Changes here
        alert('There was a problem with the DCL Validator request.');
    }
};
0
ggridleyAuthor Commented:
I saw that before I posted and tried the brackets around the block after the if, but I get the same thing.  It doesn't ever get in the http_request.readyState == 4 block anywhere.  Thanks for lookin at this!
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

ggridleyAuthor Commented:
I tried putting in alert("inside function"); just before f (http_request.readyState == 4) to see if I even get in the function at I'm not.  I think I'm missing something major here.
0
netsmithcentralCommented:
In every implementation of AJAX I've seen, the http_request.open method comes AFTER you set the http_request.onreadystatechange propert.  So:

http_request.onreadystatechange = function() {
 //do stuff
} ;
http_request.open('GET',what,false);
http_request.send(null);
0
ggridleyAuthor Commented:
Lian Liming ( http://lianliming.blog.com/ ) answered my question outside of EE.  With a Synchronous model you do NOT use the onreadystatechange.  So a functional example using the above code (which came from Lian) is here:

function makeSyncRequest(data, field){
      originalField = field;
      var fullURL = document.URL;
      var loyaltyLocation = fullURL.indexOf("/loyalty/") + 9;
      var url = fullURL.substr(0,loyaltyLocation) + "ajax?";
      http_request = false;

      if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                  http_request.overrideMimeType('text/xml');
                  // See note below about this line
            }
      } else if (window.ActiveXObject) { // IE
            try {
                  http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                  try {
                        http_request = new ActiveXObject("Microsoft.XMLHTTP");
                  } catch (e) {}
            }
      }
      if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
      }
      http_request.open('GET', url + data, false);
    http_request.send(null);
   
      if(http_request.status == 200) {
            var xmldoc = http_request.responseXML;
            var ajaxValue = xmldoc.getElementsByTagName("Root")[0].childNodes[0].nodeValue;
            if (ajaxValue == "0") {
                  alert("DCL " + validatingDCL + " is invalid.");
                  setTimeout("originalField.select()", 5);  
          }
      }
      
}

Thanks to all who tried to help out on this one, and especially to Lian.
0
GranModCommented:
PAQed with points refunded (500)

GranMod
Community Support Moderator
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.