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 ,
What are you getting from
httpRequest.responseText
isundefined
(indefinite) so can not dosplit
. It is best to put the rest of the code you are using for the request to understand where the problem comes from.– Isac
I’ve already put the whole js
– Leone Cerqueira
Make a console.log(typeof requestatual) before the split and tell us what you got on the console.
– Brunno Vianna
Status check 200 not missing =>
httpRequest.status == 200
? And why all variables? It wasn’t easier to use arrays directlyprof
andmat
?– Isac
No, @Isac, since the status may be 304 (Not modified)
– Leone Cerqueira