Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation returning text from responseText XMLHttpRequest function?

    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("");




    }

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,620
    Thanks
    78
    Thanked 4,388 Times in 4,353 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How do I accomplish this?

    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>

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,620
    Thanks
    78
    Thanked 4,388 Times in 4,353 Posts
    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:
    Code:
    <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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New to the CF scene
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Will that write over the rest of the page? I will try it tomorrow and let you know. Thank you until then.

  • #6
    New to the CF scene
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,620
    Thanks
    78
    Thanked 4,388 Times in 4,353 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •