Encountering a Uncaught TypeError: Cannot read property 'valeurs' of undefined, however the alert()
and console.log()
functions are successfully displaying the data.
Below is the code snippet:
var item = document.getElementById('inputData').value;
$.ajax({
url : 'php/modele.php',
type : 'POST',
data :{
textValue : item
},
dataType : 'json',
success : function(code_html, statut){
var newhtml = '';
console.log(code_html);
for(var i = 0; i <= code_html.length; i++){
alert(code_html[i]['valeurs']); // working but the error is pointing here. Line 21.
newhtml += i+") "+code_html[i]['valeurs']+"<br>"; // not working
console.log(code_html[i]['valeurs']); // working
}
alert(newhtml); //not displayed
document.getElementById('container').innerHTML = newhtml;
},
error : function(resultat, statut, erreur){
alert('error: '+erreur+' resultat: '+resultat);
},
complete : function(resultat, statut){
}
});
The alert()
and console.log()
functions are displaying the desired result.
Alert is working
console.log is working
However, the innerhtml
is not updating due to the error, preventing the display of data on the HTML page.
Changing this line
newhtml += i+") "+code_html[i]['valeurs']+"<br>"; // not working
to newhtml += i+") "+code_html[0]['valeurs']+"<br>"; // not working
resolves the issue and displays the value "bonjour" four times.