0

i am working on a html file with javascript code. That site uses some php files for the communication with a server (request.send...).Basically it works pretty fine, but i have some reliability issues. I found out (developer console of chrome), that sometimes there is an error like xxxx.php file not found on the server ("Failed to load resource: net::ERR_NETWORK_IO_SUSPENDED"). Looks like the server does not have sufficient availability? Now i would like to load all the files from the server when the page is loaded to have all the files cached to make it more reliable? a) is that a good idea? b) how can i tell the page to cache all the files. thanks.

function sendRequestSendCommand(nummer, text) { // Anfrage ID = 1
    // Request erzeugen
    if (window.XMLHttpRequest) {
        request = new XMLHttpRequest(); // Mozilla, Safari, Opera
    } else if (window.ActiveXObject) {
        try {
            request = new ActiveXObject('Msxml2.XMLHTTP'); // IE 5
        } catch (e) {
            try {
                request = new ActiveXObject('Microsoft.XMLHTTP'); // IE 6
            } catch (e) {}
        }
    }

    // überprüfen, ob Request erzeugt wurde
    if (!request) {
        alert("Kann keine XMLHTTP-Instanz erzeugen");
        return false;
    } else {
        var url = "SendeKommandoAnWatch.php";
        // Request öffnen
        request.open('post', url, true);
        // Requestheader senden
        request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        // Request senden
        request.send('num='+nummer+'&mycountry='+mycountry+'&text='+text);      // RD: mehrere mit & verbinden
        // Request auswerten
        request.onreadystatechange = interpretRequestSendCommand;
    }
}

function interpretRequestSendCommand() {        
    switch (request.readyState) {
        // wenn der readyState 4 und der request.status 200 ist, dann ist alles korrekt gelaufen
        case 4:
            if (request.status != 200) {                    
                if(request.status == 0)
                {
                    //alert("Es ist ein Fehler aufgetreten:\nPHP-File nicht gefunden!\nBitte versuchen sie es erneut.");
                }
                else {
                    //alert("Es ist ein Fehler aufgetreten: "+request.status);
                }
            } else {
                var content = request.responseText;
                console.log('Antwort vom Server = ' + content);
                try {
                    var response = JSON.parse(content);
                }
                catch(err)
                {
                    console.log('Fehler aufgetreten: konnte serverantwort1 nicht interpretieren');
                    state = 9;
                }   

                if(response.success == 1)
                {
                    console.log('rücksetzen der nummer an server übermittelt' + response.tokenID);      // sonst hier keine weitere aktion mehr (nicht mehr warten)
                    //console.log('tokenID vom Server = ' + response.tokenID);
                    //tokenID = response.tokenID;
                    //state = 85;                               
                }
                else
                {
                    console.log('Verbindung zum Server nicht erfolgreich! ERROR 101');                          
                    state = 9;      // error
                }

            }
            break;
        default:
            break;
    }
}
hewald
  • 21
  • 3

2 Answers2

0

Caching is usually set on the server (eg apache/nginx) If you wanted to cache the results of the ajax calls (php files you call them) you could use localstorage, although it has limited storage

In JavaScript whenever you use a library or not, you try catch the ajax request and retry every so often. If you use ES6 you could use a Promise to reject the request if it failed and catch it somewhere else later to do something more intelligent.

DarkMukke
  • 2,469
  • 1
  • 23
  • 31
  • elaborate your question with some sample code, that way i can extend my answer with some sample code as well – DarkMukke Aug 01 '17 at 08:50
  • i attached the part where i call the php file and waiting for the reply. – hewald Aug 01 '17 at 08:57
  • thanks for the idea with retry. i will try to set up some timeout and get a workaround with some kind of retry ... – hewald Aug 01 '17 at 09:22
0

I think you need to specify a (larger) timeout for your ajax calls before executing them.

request.timeout = 30000;

As for the caching, you should try the following in your htaccess as a start (if expires / headers module are available in apache):

<IfModule mod_expires.c>
    <IfModule mod_headers.c>
        #Proxy servers may cache
        Header append Cache-Control "public"
        Header append Vary: Accept-Encoding
    </IfModule>
# Enable expiration a month in future by default for everything. (this could be tuned with "ExpiresByType".)
ExpiresActive on

ExpiresDefault "access plus 1 month"
</IfModule>
twicejr
  • 1,319
  • 3
  • 13
  • 21
  • a) i dont have a htaccess!? b) the file is called every 10 seconds. i will give it a try to set a timeout like 5sec!? – hewald Aug 01 '17 at 09:05
  • i like the idea with the timeout, that will bring me forward. thanks. – hewald Aug 01 '17 at 09:21