CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript programming (http://www.codingforums.com/forumdisplay.php?f=2)
-   -   Invoking servlet with xhttp and status messages (http://www.codingforums.com/showthread.php?t=284286)

penser 12-17-2012 10:28 AM

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

devnull69 12-17-2012 01:30 PM

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


Old Pedant 12-17-2012 09:23 PM

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

}



All times are GMT +1. The time now is 03:22 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.