Solved

Synchronous AJAX needed

Posted on 2006-04-07
8
4,145 Views
Last Modified: 2008-01-09
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!
0
Comment
Question by:ggridley
8 Comments
 
LVL 2

Expert Comment

by:Andy_Neodynium
ID: 16401250
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
 
LVL 2

Expert Comment

by:Andy_Neodynium
ID: 16401267
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
 
LVL 3

Author Comment

by:ggridley
ID: 16401312
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
Independent Software Vendors: 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 3

Author Comment

by:ggridley
ID: 16401353
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
 
LVL 12

Expert Comment

by:netsmithcentral
ID: 16406273
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
 
LVL 3

Author Comment

by:ggridley
ID: 16417615
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
 

Accepted Solution

by:
GranMod earned 0 total points
ID: 16646004
PAQed with points refunded (500)

GranMod
Community Support Moderator
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

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 …
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

726 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