Seems to be everything ok, but I can't access the data inside the javascript. I know its something easy and have a lot of tutos everywhere, but I'm a little confused. Someone can help me?
Here is the three pieces of code, really simple:
foo.js
$.ajax({ data: {'termo': termo}, url: 'http://localhost/tarefa66/sysvendas/getFestas.php', success: function(data) { $('#resultado').text(JSON.parse(data)[0]); } });
getFestas.php
echo json_encode(getFestas($_GET['termo']));
dao.php
$getFestas = $pdo->query("SELECT * FROM festa f WHERE f.nome LIKE '%" . $termo . "%'"); return $getFestas->fetchAll(PDO::FETCH_OBJ);
Output on network tab (Firefox) http://i.stack.imgur.com/JfKuS.png
============================================================
Problem solved. 1. The complete method don't receive 'data', 'success' do.
The php return is something like '[{"prop": 'value'}]', so doing this 'JSON.parse(data)[0]' I was just accessing the Object inside the array - that can't be printed.
So the right use is: obj = JSON.parse(data)[0];
and after access like obj['prop']
.
I think the problem is that you're trying to set text to a javascript object instead of a string. try
complete: function(data) {
$('#resultado').text(JSON.stringify(JSON.parse(data)[0]));
}
If that doesn't work, try setting the data type to json explicitly. Then you can access data as a javascript object without parsing it yourself.
$.ajax({
data: {'termo': termo},
url: 'http://localhost/tarefa66/sysvendas/getFestas.php',
dataType: "json",
complete: function(data) {
$('#resultado').text(data[0]);
}
});
Missing closing }
from complete function..
$.ajax({
data: {'termo': termo},
url: 'http://localhost/tarefa66/sysvendas/getFestas.php',
complete: function(data) {
$('#resultado').text(JSON.parse(data)[0]);
}
});