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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Invoking servlet with xhttp and status messages

    I have an issue to save a long base64 String to a database/file.
    I would like to show some message/status in a text field like 'Writting...' before and than use download function (which calls a servlet) like this:

    function doDownload(pageId) {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function () {
    displayStatus('Ready');
    };
    xhttp.open("GET", "http://127.0.0.1:7101/testApp/images?pageId=" + pageId, false);
    xhttp.send();
    }


    However, when I call a function to display status 'Writting' at the beginning:

    function displayStatus(status) {
    var field = AdfPage.PAGE.findComponentByAbsoluteId('itStatus');
    if (field) {
    field.setValue(status);
    }
    }


    It is not displaying the message 'Writting' (probably because it all happens in a one thread.

    Could you forum users tell me how to show a status message before really invoking servlet action?

    kind regards,
    Christopher

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    Usually it helps to give the browser "some time" to perform the action before you start the request, even more so if you are starting a synchronous request which will freeze the browser.

    Code:
    displayStatus("Writing...");
    window.setTimeout(function() {doDownload(whatever); }, 300);

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,211
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    If you would change to an ASYNCHRONOUS call, you'd solve THREE problems at once.

    (1) Your "Waiting.." would display as soon as send() is called.
    (2) You would not be freezing JavaScript while waiting for the send to complete.
    (3) You could actually check to see if the operation completed normally.

    Code:
    function doDownload(pageId) 
    {
        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function ()
        { 
            if (xmlhttp.readyState==4 )
            {
                displayStatus (  ( xmlhttp.status==200) ? "Ready" : "Error " + xmlhttp.status );
            }
        }
        xhttp.open("GET", "http://127.0.0.1:7101/testApp/images?pageId=" + pageId, true);
        xhttp.send();
        displayStatus('Waiting'); // this line could actually be first in this function or before send...makes no difference
    
    }
    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.


  •  

    Tags for this Thread

    Posting Permissions

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