发现错误 - 混合:php 数据对象(获取对象)、json 和 jquery.post()
任何比我更有经验的人都可以在这里发现问题吗? 我无法调试它,由于使用 jquery.post(),我的 var_dumps 似乎没有任何效果,所以我相信。
我什么也没显示。我期待收到一个充满 json 编码值的 li 系列。
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>
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;
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();
}
}
如果这个位置变得很难捕获,我怎样才能发现它。这是我第一次使用ajax,所以我对调试部分不太适应。 :s
怀疑:(更新) 我相信问题出在我尝试迭代返回的 json 的方式上。 这是 json_encoded 的 echo 格式:
[{"nomeDominio":"aaaa.ka"},{"nomeDominio":"agentesdeexecucao.ka"}]
非常感谢, MEM
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于对象的基础是一个数组,您需要在根级别迭代它,因此您的
for
循环应如下所示:或者
$.each()
路线:重要的部分是
resposta.nomeDominio
不是'没有任何东西,因为响应的根是一个数组,但是resposta.length
将获得长度,所以使用它。另外,由于数组位于根目录,并且其中的每个对象都有一个nomeDominio
属性,因此您希望resposta[x].nomeDominio
转到当前索引(以获取对象),然后调用.nomeDominio
来获取属性。或者,使用$.each()
路由,其中this
指的是当前对象,无论哪种方式都有效。Since the base of the object is an array you need to iterate over it at the root level, so your
for
loop should look like this:Or the
$.each()
route:The important part is that
resposta.nomeDominio
isn't anything, since the root of the response is an Array, howeverresposta.length
will get the length, so use that. Also since the array is at the root and each object in it has anomeDominio
property, you wantresposta[x].nomeDominio
to go to the current index (to get the object), then call.nomeDominio
to get the property. Or, use the$.each()
route in whichthis
refers to the current object, either way works.