473,804 Members | 3,067 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

I have several functions with code along the lines of:

var xmlDoc = requestXML("aja x.asp?SP=Select RelatedTags&tag =" +
array[i]);

The requestXML() function includes the code:

var xmlDoc = null;
http_request.on readystatechang e = function() {
if (http_request.r eadyState == 4) {
if (http_request.s tatus == 200) {
xmlDoc = http_request.re sponseXML;
} else {
alert('There was a problem with the request.' +
http_request.st atus);
}
}
};
http_request.op en('GET', url, true);
http_request.se nd(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

Dec 4 '06 #1
1 2149
iporter wrote:
I have several functions with code along the lines of:

var xmlDoc = requestXML("aja x.asp?SP=Select RelatedTags&tag =" +
array[i]);

The requestXML() function includes the code:

var xmlDoc = null;
http_request.on readystatechang e = function() {
if (http_request.r eadyState == 4) {
if (http_request.s tatus == 200) {
xmlDoc = http_request.re sponseXML;
} else {
alert('There was a problem with the request.' +
http_request.st atus);
}
}
};
http_request.op en('GET', url, true);
http_request.se nd(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.
Once you start http_request, all the communication between the browser
and server happens in the background. This is the reason return xmlDoc
isn't passing anything back.

http_request.on readystatechang e defines a function that is called by
http_request when the ready state changes. So what you need to do is
to make that line read...

http_request.on readystatechang e = function() {
if (http_request.r eadyState == 4) {
if (http_request.s tatus == 200) {
xmlDoc = http_request.re sponseXML;
processData(xml Doc);
} else {
alert('There was a problem with the request.' +
http_request.st atus);
}
}
}
Notice I added the line "processData(xm lDoc)". This will call a
function you make (named processData) which will receive the response,
allowing you to process the return data.

If you TRULY want the browser to basically do nothing until it hears
back from the server then you need to scrap all this code and research
synchronous javascript and xml :)
---------------------------------------------------------------------------
http://www.hunlock.com -- Permanently under construction (And proud of it!)
$FA
Dec 4 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
13355
by: Sanjay Dahiya | last post by:
I tried POSTing from XMLHttpRequest, i can get the XML right on server but responseXML from server is coming null. I can see the XML right in responseText. but responseXML is null. responseText to DOM conversion also fails while the XML in responseText seems valid .. -- here is the javascript code for sending --- { this.request.onreadystatechange = this.handleStateChange; if( this.request) {
3
9568
by: Noozer | last post by:
Hrm.. last posting was mangled. Let's try again, with more detail... I'm just starting to try out "Ajax" web programming and I've got a question. AJAX is fairly straightforward. Javascript creates an XMLHttp object and then uses that to generate a hidden webpage with your results. What I'd like to know is, how can I retrieve multiple values from an XMLHttp request?For example, if my request generated an SQL query that returned a...
7
5014
by: KaNos | last post by:
Hello aspx world, I consume a webservice with Javascript functions. But the response is ok and, very strange, document is null. How to resolve this problem thanks ?????? xmlHttpRequest.open("POST", myAddressLocation, false); xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); xmlHttpRequest.send(xmldocumentSoapRequest);
6
4271
by: wizard04frms | last post by:
I'm having trouble figuring out what's going on with IE6's Msxml2.XMLHTTP object. I have two feed addresses in this stripped down version of my code below. Both work fine in Firefox (using the XMLHttpRequest object), but only the thinkgeek one works in IE. In the processFeed function, it shows the problem - the first alert shows 0 for the wikihow feed in IE, though it can still display the responseText. Any insight? //var feedAddr...
1
1968
by: MD | last post by:
Following snippet is the portion of my program to get the location list in xml with AJAX when i alert(request.responseText) it manages to display the xml document like the following <? xml version="1.0" ?> <markers> <marker lat="32.564" lng="133.212" /> </markers>
5
8863
by: furby | last post by:
I have this little bit of code : var request = false; var divid = 0; var fforie; try { request = new XMLHttpRequest(); fforie = "FF"; } catch (trymicrosoft) { try { //request = new ActiveXObject("Microsoft.XMLHTTP");
14
6078
by: webEater | last post by:
I downloaded IE7 and tried out the native XMLHttpRequest support. I think there is an object in IE7 called "XMLHttpRequest" but I ask me why it is called "XML"HttpRequest. I made a request to an XML file (correctly sent with text/xml header) but IE7 returns the same shit as IE 5.5/6 when I use responseXML, an XML construct with a node "xml", which contains nothing. Firefox and Opera and Netscape 7.1 (yeah,) do it correctly. What's goin...
1
3108
by: Sand Yaah | last post by:
i went thru a discussion put by eros and helped out by dmjpros. d questions asked were right and i tried each but there was no problem there. my code returns null in xmlHttp.responseXML and xmlHttp.responseText returns nothing. My Code: function commoPrice() { var cid = document.kerala.commodities.value; if(cid ==0)
6
9833
by: KDawg44 | last post by:
Hi, My responseXML is always null on my AJAX call. When I browse directly to the PHP script I am calling, the XML file shows up just fine. I have read that if a returned XML file is not valid, it will always appear as null. I am just returning some basic XML like: <?xml version='1.0' encoding='UTF-8'?> <XMLData>
0
9704
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9569
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10318
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9130
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2975
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.