2
Okay, guys, here’s my problem. I have a JS function to make a query in mysql database via ajax, it returns apparently correctly but when I try to access the value of "Undefined" This is an Alert(Sponse); being "Answer" ajax response
however at the time I try to access example Answer[0]. student, result is Undefined.
JS
var hora = $this.find('h1').text();
$.ajax({
type: "POST",
dataType: "html",
url: "alunos.php",
data: { 'hora': hora},
success: function(response){
alert(response);
// for(var i=0;response.length>i;i++) {
// console.log( response[i].nome);
// }
}
});
<?php
header("Content-Type: text/html; charset=UTF-8");
$mysqli = new mysqli('localhost', 'root', 'vagrant', 'webfit');
$hora = filter_input(INPUT_POST, 'hora');
$sql = "SELECT aluno.nome as aluno from aula, aluno_aula,aluno where aluno.id = aluno_aula.aluno_id AND aluno_aula.aula_id=aula.id AND aula.horaini='{$hora}';"; //monto a query
$query = mysqli_query($mysqli,$sql); //executo a query]
while($row = mysqli_fetch_assoc($query)){
$vetor[] = array_map('utf8_encode', $row);
}
echo json_encode($vetor);
?>
PHP
If I make any of these changes I get: [Object Object],[Object Object] in the place where I received what is represented in Alert. That’s what’s bothering me these changes should work.
– R. Pêgo
But that’s right, now you have to manipulate the object.
– Wictor Chaves
I will make an example and post here in the reply, just a minute.
– Wictor Chaves
I updated the example
– Wictor Chaves
Dude, thank you so much for this idea of JSON.parse(string), solved as follows: var data = JSON.parse(Answer);
– R. Pêgo