...

View Full Version : returning text from responseText XMLHttpRequest function?



jazzyice
04-23-2010, 09:21 PM
I am wondering how can I return the variable senttext from the function below?

I have declared it and when I alert(senttext) below it appears as I want it but I need to return it and pass it to another function. When I change alert() to return the variable becomes undefined???


var senttext;
function getData()
{

if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
}
else if (window.ActiveXObject) {

req = new ActiveXObject("Microsoft.XMLHTTP");
}

req.open('GET',"http://www.site6.certcoordering1.com/Download/recall.txt" );



req.onreadystatechange = function()
{
if (req.readyState == 4)
{
if (req.status == 200)
{

senttext ='<table style="border:thin black solid"><tr><td><h2>Recent</h2></td><td><h2>Recalls</h2></td><td></td><td></td></tr>' ;


var ad2 = req.responseText.split("\n");
for(i=0; i<ad2.length-1; i++)
{

// now split by comma
var ad3 = ad2[i].split(",");
senttext += '<tr><td><p>'+ad3[0]+'</p></td><td> <a href="http://www.site6.certcoordering1.com/Download/'+ad3[2]+' target="_blank">'+ad3[1] +'</a></td><td></td></tr>'



}


senttext += '</table>' ;
alert(senttext);

// I need to return senttext, not alert! When I send it it becomes undefined?
}
else
{
alert('File did not load correctly.');
}
}
}

req.send("");




}

Old Pedant
04-23-2010, 09:47 PM
return it to where???

You can't just return it from the function, because your code didn't call the function (the event handler of the req object called it).

And in any case it might "return" several milliseconds after the original function was called.

One way you could do this would be to provide a callback function that would be invoked when the response was received.

jazzyice
04-24-2010, 02:10 AM
What I am trying to accomplish:

I have a comma delimited text file on the server. I want to dynamically populate a table with the values from the text file on the page.

I seem to understand what is going on with the XMLHTTPREQUEST object but I can't seem to be able to return anything valuable.

Below is what I have so far. I am trying to populate the senttext variable with the return result from the responseText. Thank you for the comment about the event handler. I was unaware of this. How do I invoke a function to get this result?

Here is what I am trying to do:

<script type="text/javascript">
<!--

var senttext='';
function getData()
{

if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
}
else if (window.ActiveXObject) {

req = new ActiveXObject("Microsoft.XMLHTTP");
}

req.open('GET',"http://www.site6.certcoordering1.com/Download/recall.txt" );



req.onreadystatechange = function()
{
if (req.readyState == 4)
{
if (req.status == 200)
{

senttext ='<table style="border:thin black solid"><tr><td><h2>Recent</h2></td><td><h2>Recalls</h2></td><td></td><td></td></tr>' ;


var ad2 = req.responseText.split("\n");
for(i=0; i<ad2.length-1; i++)
{

// now split by comma
var ad3 = ad2[i].split(",");
senttext += '<tr><td><p>'+ad3[0]+'</p></td><td> <a href="http://www.site6.certcoordering1.com/Download/'+ad3[2]+' target="_blank">'+ad3[1] +'</a></td><td></td></tr>'



}


senttext += '</table>' ;
alert(senttext);

// I need to return this
}
else
{
alert('File did not load correctly.');
}
}
}

req.send("");




}



function getPass()
{


var se = getData();

document.write(se);

}

//-->

</script>




<table><tr><td><script type="text/javascript">getPass()</script>
</td></tr></table>

Old Pedant
04-24-2010, 02:42 AM
Returning something from that function won't do you any good.

You would need to document.write() it in order to stick it in place there in your HTML.

Now, that *is* possible, but only if you used a *SYNCHRONOUS* call (instead of asynch), but that means that the creation of the page would "hang" at that point until the function returned.

If that's acceptable to you, then you could just document.write() that stuff instead of returning it.

Something like this:


<script type="text/javascript">
function getData()
{
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
req.open('GET',"http://www.site6.certcoordering1.com/Download/recall.txt", false );
req.send( null );
document.write('<table style="border:thin black solid"><tr><td><h2>Recent</h2></td><td><h2>Recalls</h2></td><td></td><td></td></tr>') ;
var ad2 = req.responseText.split("\n");
for(i=0; i<ad2.length-1; i++)
{
// now split by comma
var ad3 = ad2[i].split(",");
document.write(<tr><td><p>'+ad3[0]+'</p></td><td> <a href="http://www.site6.certcoordering1.com/Download/'+ad3[2]+' target="_blank">'+ad3[1] +'</a></td><td></td></tr>');
}
document.write( '</table>' );
}
</script>

...


<table><tr><td><script type="text/javascript">getPass()</script>
</td></tr></table>

By passing false as the 3rd parameter to the open() function, you are telling the object to simply wait, doing nothing, until the data is ready.

jazzyice
04-24-2010, 03:30 AM
Will that write over the rest of the page? I will try it tomorrow and let you know. Thank you until then.

jazzyice
04-24-2010, 03:37 AM
It works! I couldn't help myself! Thank you so much. I will sleep better now. Is there any good literature on the difference between async and sync calls?

Old Pedant
04-24-2010, 09:55 PM
async means that the rest of the page works independently of the AJAX code. So other things using JavaScript can continue to run. sync means that the whole page just sits there waiting for the AJAX (well, technically, not AJAX any more...SJAX?) to finish.

I wouldn't normally use synch exec while a page is loading, but that means it should be fine for what you are doing here.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum