If I have something like this from the server side, from a fetch:
array(1) { [0]=> array(1) { ["nome"]=> string(7) "aaaa.br" } } [{"nome":"aaaa.br"}]
The json of the above is:
[{"nome":"aaaa.br"}]
This Works:
parse: function(data) {
return $.map(eval('('+data+')'), function(result) {
return {
data: result,
value: result.nome,
result: result.nome
}
});
}
The result is parsed successfully.
If, instead of fetch, I change to fetchAll, the dump gets like this (here only the first index as example):
array(65) { [0]=> array(1) { ["nome"]=> object(stdClass)#7 (1) { ["nomeDominio"]=> string(7) "aaaa.br" } }
The json conversion of the above:
string(2632) "[{"nome":{"nomeDominio":"aaaa.br"}}
Here, the result is not successfully parsed.
So I believe something needs to be changed on the js side. But I'm absolutely clueless.
UPDATE: The nomeDominio is from the fetchObj PDO method, and corresponds to the column name on the database. It's a natural behaviour for fetch with PDO when FETCH::OBJ option is used.
The php part of this js is:
$keyword = addslashes($_GET["q"]);
$comandos = new ComandoController();
$arr = $comandos->recebeNomeDominios($keyword);
if(is_array($arr))
{
echo json_encode($arr);
}
public function recebeNomeDominios($keyword)
{
$DominioDao = new DominioDao();
$objecto = $DominioDao->recebeNomeDominios($keyword);
return $this->jsonArray($objecto);
}
private function jsonArray($objecto)
{
$json = array();
if(isset($objecto) && !empty($objecto))
{
foreach($objecto as $obj)
{
$json[] = array('nome' => $obj);
}
}
return $json;
}
Finally:
public function recebeNomeDominios($keyword)
{
try
{
$stmt = $this->_dbh->prepare("SELECT d.nomeDominio FROM dominio d WHERE d.nomeDominio LIKE '%".$keyword."%'");
$stmt->execute();
$resultado = $stmt->fetch(PDO::FETCH_OBJ);
return $resultado;
}
catch (PDOException $ex)
{
echo "Erro: " . $ex->getMessage();
}
}
Any advice? MEM