Uncaught TypeError: Can not read property 'split' of undefined at XMLHttpRequest.alertContents

1

 var prof1 = 0, prof2 = 0,prof3 = 0, prof4 = 0,prof5 = 0, prof6 = 0,prof7 = 0, prof8 = 0,prof9 = 0, prof10 = 0,prof11 = 0, mat1 = 0,mat2 = 0, mat3 = 0, mat4 = 0,mat5 = 0, mat6 = 0, mat7 = 0,mat8 = 0, mat9 = 0, mat10 = 0,mat11 = 0;
    var requestant, requestatual,prof,mat;
    requestant = '';
    window.setInterval(makeRequest, 10000);
    httpRequest = new XMLHttpRequest();
    function makeRequest() {
        httpRequest.open('GET', 'result.php');
        httpRequest.send();
        if (httpRequest.readyState === 4) {
            httpRequest.onreadystatechange = alertContents;
        }
    }
    function alertContents() {
        if (requestant !== requestatual) {
            if (requestatual !== '') {
                if (requestatual !== undefined) {
                    requestatual = httpRequest.responseText;
                    requestatual = requestatual.split(';');
                    prof = requestatual[0].split(',');
                    mat = requestatual[1].split(',');
                    prof1 = prof[0];
                    prof2 = prof[1];
                    prof3 = prof[2];
                    prof4 = prof[3];
                    prof5 = prof[4];
                    prof6 = prof[5];
                    prof7 = prof[6];
                    prof8 = prof[7];
                    prof9 = prof[8];
                    prof10 = prof[9];
                    prof11 = prof[10];
                    mat1 = mat[0];
                    mat2 = mat[1];
                    mat3 = mat[2];
                    mat4 = mat[3];
                    mat5 = mat[4];
                    mat6 = mat[5];
                    mat7 = mat[6];
                    mat8 = mat[7];
                    mat9 = mat[8];
                    mat10 = mat[9];
                    mat11 = mat[10];
                    requestant = requestatual;
                    grafcs();
                }
            }
        }
    }

Error

Uncaught TypeError: Cannot read property 'split' of undefined at XMLHttpRequest.alertContents

Response that result.php gives

1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , ;0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
    
asked by anonymous 02.09.2017 / 00:06

2 answers

1

Your code has problems receiving XMLHttpRequest .

You are checking readyState before loading the return. The correct would be:

function makeRequest() {
        httpRequest.open('GET', 'result.php');
        httpRequest.send();
        httpRequest.onreadystatechange=function(){ //callback quando o retorno estiver pronto
            if (httpRequest.readyState === 4) { //processo concluído
                requestatual = httpRequest.responseText; // atribuo os dados recebidos à variável
                alertContents(); // chamo a função
            }
        }
    }
    
02.09.2017 / 04:26
1

It could be that:

  • requestatual may not be a string
  • requestatual is undefined
  • httpRequest is not receiving information

Note: Show more code next time.

    
02.09.2017 / 00:11