Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Synchronous AJAX needed

Posted on 2006-04-07
8
Medium Priority
?
4,172 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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

972 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