Ajax response repeats that of another request and the page is neither the same

3

The scenario is as follows: I have an ajax waiting for a response from the Java server on port 2336 just like Hotmail, and as soon as it has an update, Java returns a response and closes the open HTTP request. Then the javascript picks up the response and executes whatever it is and opens another request again to wait again for new updates. When I have to send something to the server I send PHP to port 80, usually on another request, and PHP returns the response whether it was successful or not. So, okay, the problem is that if the synchronization request with Java on port 2336 is open and I send something to port 80 when it arrives some warning of authallation of Java the answer is the same as that of PHP. Then if I have not sent anything to PHP again, it shows the Java response normally.

Does anyone have an idea of what might be happening?

The functions are as follows:

function sincronizar() {
    ajax("http://" + window.location.hostname + ":2336", "POST", "Id=Anderson", executaResposta, "");
}

function controle() {
    ajax("php/controle.php", "POST", "Comando=ID", "containerFiltragemEntregas", "");
}

function ajax(endereco, metodo, dados, alvo, distracao) {
    xmlhttp = new XMLHttpRequest();
    if (distracao != "") {
        centralizaElemento(distracao);
        mostraElemento(distracao);
    }
    if (typeof(alvo) == "string") {
        elemento(alvo).innerHTML = "";
    }

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var retorno = xmlhttp.responseText.trim();
            mostraAlerta(retorno + "\r\n\r\n" + metodo);
            if (typeof(alvo) == "function") {
                alvo(retorno);
                } else {
                    elemento(alvo).innerHTML = retorno;
            }
            if (distracao != "") {
                centralizaElemento(distracao);
                escondeElemento(distracao, 2000);
            }
        }
    }

    if (metodo == "GET") {
        xmlhttp.open("GET", endereco + "?" + dados, true);
        xmlhttp.send();
    } else if (metodo == "POST") {
        xmlhttp.open("POST", endereco, true);
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlhttp.send(dados);
    }
}

function executaResposta(resposta) {
    var respostaSeparada = resposta.split("|");
    var comandoSeparado;
    if (respostaSeparada[0] == "01") {
        if (respostaSeparada[1] == "01") {
            comandoSeparado = respostaSeparada[3].split("&");
            if (respostaSeparada[2] == "05") {
                alertaEntrega("containerEntrega" + comandoSeparado[0]);
            } else if (respostaSeparada[2] == "04") {
                escondeElemento("containerEntrega" + comandoSeparado[0]);
            } else {
                normalizaEntrega("containerEntrega" + comandoSeparado[0]);
            }
            elemento("entregaSituacao" + comandoSeparado[0]).innerHTML = comandoSeparado[1];
        }
     }
    sincronizar();
}
    
asked by anonymous 21.09.2014 / 17:33

1 answer

6

The problem is that you use a single XMLHttpRequest object. I may be wrong, but I think to correct just make your variable xmlhttp local (because it is a global implicit ) ):

function ajax(endereco, metodo, dados, alvo, distracao) {
    var xmlhttp = new XMLHttpRequest();
    // ... O resto fica igual
}

Thus, each call of ajax will create a xmlhttp independent variable, and the requests will not interfere with each other.

    
21.09.2014 / 17:47