Visualizzazione dei risultati da 1 a 10 su 10

Discussione: XHR timeout

  1. #1

    XHR timeout

    Ho una chiamata XMLHTTP del tipo:

    codice:
    dojo.connect(jsonStoreSpecificAttributes, "onSet", function(item)
        {
        if (!confirm('Salvare le modifiche? '))  return false;
    
        var uri = "interface.controlCenter.php?updateSpecifics&bookletId=<?php echo $bookletId;?>";
        if (typeof XMLHttpRequest!="undefined") xmlhttp = new XMLHttpRequest();
        else if (typeof window.ActiveXObject!="undefined") xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        else return false;
    
        xmlhttp.open("POST",uri,true);
        xmlhttp.timeout = 30000;
        xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=ISO-8859-15");
        xmlhttp.setRequestHeader("Connection","close");
    
        xmlhttp.onreadystatechange = function()
            {
            if (xmlhttp.readyState==4)
                if (xmlhttp.responseText!="") alert(xmlhttp.responseText); // in case of error.
            }
        var data = "key="+escape(item.key)+"&"+"value="+escape(item.value);
        xmlhttp.send(data);
        return true;
        });
    Come si gestisce correttamente un timeout, nel caso in cui appunto la risorsa richiesta non risponda?
    &nbsp;
    :: Instant WebKiosk, a browser-only Linux operating system ::

  2. #2
    E' un po datata ma tanto per
    darti l'idea:

    Codice PHP:
    var maxSecondsWait5;
    var 
    timeOutCheckWait500;
    var 
    timeoutMsg1'Il server tarda a rispondere!';
    var 
    timeoutMsg2'Ritentare fra qualche secondo o segnalare il problema all assistenza';
    function 
    XHConn(){
        var 
    xmlhttp null;
        var 
    resulttrue;
        var 
    browser navigator.userAgent.toUpperCase();
        if(
    typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object"){
                    
    xmlhttp = new XMLHttpRequest();
        }
        else if(
    window.ActiveXObject && browser.indexOf("MSIE 4") < 0){
            if(
    browser.indexOf("MSIE 5") < 0){
                
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            }
            else{
                
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
        }
        if (
    xmlhttp===null){resultfalse;}
        
    this.connect = function(sURLmMethodsVarsfnDonefnError){
            var 
    callupDate = new Date();
              var 
    callupStart callupDate.getTime();
              var 
    checkWaitInitialized false;
            var 
    checkSpentTime=function(){};
              var 
    maxWait;
            var 
    timeoutID0;
            var 
    sMethod mMethod.toUpperCase();
            
    xmlhttp.open(sMethodsURLtrue);
            
    xmlhttp.setRequestHeader("connection""close");
            if(
    sMethod == "POST"){
                
    xmlhttp.setRequestHeader("Method""POST "+sURL+" HTTP/1.1");
                
    xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
                
    xmlhttp.setRequestHeader('Content-Length'sVars.length);
            }
            
    xmlhttp.onreadystatechange = function(){
                if (
    xmlhttp.readyState == 4){
                    if(
    xmlhttp.status == 200){
                        
    window.clearTimeout(timeoutID);
                        
    fnDone(xmlhttp);
                       }
                    else{
                        
    fnError(xmlhttp);
                    }
                }
                else if(!
    checkWaitInitialized){
                    
    maxWaitmaxSecondsWait 1000;
                    
    checkSpentTime= function(){
                        
    callupDate = new Date();
                        if((
    callupDate.getTime() - callupStart) > maxWait) {
                            
    //Expired time!!
                            
    xmlhttp.onreadystatechange= function(){return;};
                            
    xmlhttp.abort();
                            
    alert(timeoutMsg1);
                            
    alert(timeoutMsg2);
                              
    location.reload(true);
                        } 
                        else {
                            
    timeoutIDwindow.setTimeout(checkSpentTimetimeOutCheckWait);
                        }
                      };
                    
    checkSpentTime();
                    
    checkWaitInitialized true;
                    
                  }
            }
            
    xmlhttp.send(sVars);
        }
        return 
    result;
    };
    function 
    errorHandler(xhr){
        
    alert("HTTP error: "+xhr.status);
    }
    /*
    Esempio
    function Miafunzione(xhr){
    alert(xhr.responseText);
    }
    var xhr= new XHConn();
    if(!xhr){alert(noXhr);return;}
    var postVar= 'JSON='+encodeURIComponent(json_encode(values));
    xhr.connect(page,'POST',postVar,Miafunzione,errorHandler);
    */ 

    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  3. #3
    Grazie mille per la risposta e per avermi indicato il metodo esatto.
    Ho trovato però che il seguente funziona meglio:

    codice:
    dojo.connect(jsonStoreSpecificAttributes, "onSet", function(item)
        {
        if (!confirm('Salvare le modifiche? '))  return false;
    
        var uri = "interface.controlCenter.php?updateSpecifics&bookletId=<?php echo $bookletId;?>";
        if (typeof XMLHttpRequest!="undefined") xmlhttp = new XMLHttpRequest();
        else if (typeof window.ActiveXObject!="undefined") xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        else return false;
    
        xmlhttp.open("POST",uri,true);
    
        xmlhttp.timeout = 30000;
        xmlhttp.ontimeout = xmlhttpct.abort();
    
        xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=ISO-8859-15");
        xmlhttp.setRequestHeader("Connection","close");
    
        xmlhttp.onreadystatechange = function()
            {
            if (xmlhttp.readyState==4)
                if (xmlhttp.responseText!="") alert(xmlhttp.responseText); // in case of error.
            }
        var data = "key="+escape(item.key)+"&"+"value="+escape(item.value);
        xmlhttp.send(data);
        return true;
        });
    &nbsp;
    :: Instant WebKiosk, a browser-only Linux operating system ::

  4. #4
    Quel codice abortisce la chiamata (dopo il timeout non viene più richiesta la risorsa HTTP), però mi immobilizza FF nell'usare una seconda volta quell'oggetto per una ulteriore chiamata.

    Why????
    &nbsp;
    :: Instant WebKiosk, a browser-only Linux operating system ::

  5. #5
    Grazie al c.... che funziona meglio
    stai utilizzando un framework.
    (non lo avevo visto nel primo post)
    Dojo non lo conosco ma puoi benissimo
    farlo con jquery (che tra le altre cose è molto + leggero come fw)
    Afaik dojo viene utilizzato per progetti enterprise.


    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  6. #6
    No non sto utilizzando le primitive di dojo, solo il dojo.connect è un costrutto dojo; quello che c'è "dentro" è una chiamata XHR std - dojo avrebbe la sua ma per comodità mia preferisco usare una XHR "plain".

    .. però non va a dovere ;I

    PS: è un progetto enterprise...
    &nbsp;
    :: Instant WebKiosk, a browser-only Linux operating system ::

  7. #7
    Originariamente inviato da emmebì
    No non sto utilizzando le primitive di dojo, solo il dojo.connect è un costrutto dojo; quello che c'è "dentro" è una chiamata XHR std - dojo avrebbe la sua ma per comodità mia preferisco usare una XHR "plain".

    .. però non va a dovere ;I

    PS: è un progetto enterprise...


    Codice PHP:
    if (!confirm('Salvare le modifiche? '))  return false;

        var uri = "interface.controlCenter.php?updateSpecifics&bookletId=<?php echo $bookletId;?>";
        if (typeof XMLHttpRequest!="undefined") xmlhttp = new XMLHttpRequest();
        else if (typeof window.ActiveXObject!="undefined") xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        else return false;

        xmlhttp.open("POST",uri,true);
    Torno a ripetere che dojo non lo conosco
    ma so che se facessi una cosa del genere
    con jquery mi sputerebbe in un occhio

    Ho i miei dubbi che dojo non abbia
    un metodo che faccia al caso tuo.


    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  8. #8
    Originariamente inviato da whisher
    Ho i miei dubbi che dojo non abbia
    un metodo che faccia al caso tuo.
    Ce l'ha ce l'ha, sono io che per motivi miei (e testardaggine...) vorrei usare un XHR plain
    &nbsp;
    :: Instant WebKiosk, a browser-only Linux operating system ::

  9. #9
    Originariamente inviato da emmebì
    Ce l'ha ce l'ha, sono io che per motivi miei (e testardaggine...) vorrei usare un XHR plain
    d'accordo che fare tutto a manina
    ha il suo fascino ma ..................
    a tutto c'è un limite


    Semplice costatazione
    Secondo te il tuo codice
    è migliore di quello di dojo ?

    Ho i miei forti dubbi.


    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  10. #10
    Originariamente inviato da whisher
    è migliore di quello di dojo ?
    Ah direi di no!
    &nbsp;
    :: Instant WebKiosk, a browser-only Linux operating system ::

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.