Uncaught Typeerror: Cannot read Property 'split' of Undefined at Xmlhttprequest.alertContents

Asked

Viewed 420 times

1

I am making an AJAX request, via GET and putting in variable httpRequest and I’m trying to use a .split(';') in the text response this is an excerpt from the code I’m using:

 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

Answer 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 ,
  • 1

    What are you getting from httpRequest.responseText is undefined(indefinite) so can not do split. It is best to put the rest of the code you are using for the request to understand where the problem comes from.

  • I’ve already put the whole js

  • 1

    Make a console.log(typeof requestatual) before the split and tell us what you got on the console.

  • 1

    Status check 200 not missing => httpRequest.status == 200 ? And why all variables? It wasn’t easier to use arrays directly prof and mat?

  • No, @Isac, since the status may be 304 (Not modified)

2 answers

1


Your code has problems receiving the XMLHttpRequest.

Are you checking the 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
            }
        }
    }

1

It may be that:

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

Obs: Show more code next time.

  • I didn’t give an Alert() it’s all ok

  • 1

    @Leonecerqueira you called the makeRequests()?

  • I called yes ta la no js

  • 1

    i n tou vendo la @Leonecerqueira

  • 1

    ah, I’ve seen it try setInterval(function() {makeRequest();}, 10000);

  • I tried, but it didn’t solve, because the problem isn’t in the answer, nor in the setInterval loop, but in the split

  • @Leonecerqueira gives a console.log in the requestatual

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.