Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Ajax Programming

Returning responseXML, set within an anonymous function, only once readyState = 4


I have several functions with code along the lines of:

var xmlDoc = requestXML("ajax.asp?SP=SelectRelatedTags&tag=" +
array[i]);

The requestXML() function includes the code:

var xmlDoc = null;
http_request.onreadystatechange = function() {
        if (http_request.readyState == 4) {
                if (http_request.status == 200) {
                    xmlDoc = http_request.responseXML;
                } else {
                    alert('There was a problem with the request.' +
http_request.status);
                }
            }
    };
http_request.open('GET', url, true);
http_request.send(null);
return xmlDoc;

However, the last line (the return) executes before the readyState
reaches 4.  How do I return the xmlDoc to the functions only once the
xmlDoc has been assigned?  I tried putting the return statement in a
while loop with the condition that the readyState must = 4 - this
worked, but makes the browser popup a message saying the script is
slowing down the system.

Thanks,
Iain

Like the all of the other posts, what you are trying to do is an sychronous
request and not a asychronous request.  A asychronous (true flag in open
method) keeps processing so that is why your return brings back null. Almost
any Ajax book or article will show you how to use a closure to bring back
data. Look into prototype.js or dojo.

Eric

On 12/4/06, iporter <ispor@gmail.com> wrote:

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc