Can anyone better experienced then me, spot the issue here.
I’m unable to debug it, my var_dumps don’t seem to get any effect due to the use of jquery.post() so I believe.
I get nothing displayed. I was expecting to receive a li series filled with json encoded values.
The HTML:
<div id='data'></div>
<form action="">
<input id="nomeInput" type="text" name="nomeInput" value="" autocomplete="false"/>
</form>
<ul id="listaDominios" style="display: none;">
</ul>
The js:
$(document).ready(function(){
$('#nomeInput').keypress(function(){
$.post("testeBasico_1.php", {nomeInput : $('#nomeInput').val()}, function(resposta) {
for (var x = 0, tamanhoDados = resposta.nomeDominio.length; x < tamanhoDados; x++){
$('#listaDominios').show();
$('#listaDominios').append('<li>'+resposta.nomeDominio[x]+'</li>');
}
}, "json");
});//end of keypress;
});//end of document ready;
The PHP
public function listaDominios(DominioVo $dominioVo)
{
try
{
$stmt = $this->_dbh->prepare("SELECT d.nomeDominio FROM dominio d WHERE d.nomeDominio LIKE ?");
$stmt->bindValue(1,'%' . 'a' . '%', PDO::PARAM_STR);
$stmt->execute();
$resultado = $stmt->fetchAll(PDO::FETCH_OBJ);
return $resultado;
}
catch (PDOException $ex)
{
echo "Erro: " . $ex->getMessage();
}
}
If the spot gets to be a difficult catch, how can I spot it. It’s my first ajax experience, so I’m not comfortable with the debugging part. :s
Suspicion: (UPDATE)
I believe the issue is in the way I’m trying to iterate over the returned json.
Here’s the echo format of the json_encoded:
[{"nomeDominio":"aaaa.ka"},{"nomeDominio":"agentesdeexecucao.ka"}]
Thanks a lot in advance,
MEM
Since the base of the object is an array you need to iterate over it at the root level, so your
forloop should look like this:Or the
$.each()route:The important part is that
resposta.nomeDominioisn’t anything, since the root of the response is an Array, howeverresposta.lengthwill get the length, so use that. Also since the array is at the root and each object in it has anomeDominioproperty, you wantresposta[x].nomeDominioto go to the current index (to get the object), then call.nomeDominioto get the property. Or, use the$.each()route in whichthisrefers to the current object, either way works.