{"autor":"Testando","publication":"a"}{"autor":"Testando","publication":"a"}{"autor":"Testando","publication":"asa"}{"autor":"Testando","publication":"a"}
PHP;
header('Content-Type: application/json');
while ($rows = $stmt->fetch(PDO::FETCH_ASSOC)) {
$autor = $rows['Nome'];
$publication = $rows['publication'];
$my = array(
"autor"=>"$autor",
"publication"=>"$publication"
);
echo json_encode($my);
}
dataType: "json",
success: function (data) {
$(data).each(function(autor, pubication) {
alert(data.autor + " : " + data.publication)
});
}
我的问题:jQuery没有提醒数据
我认为问题是因为结果之间没有逗号,因为当只有一个结果时,他通常会在将来警告想要在数据中创建.append,但我不能简单地提醒数据,可以有人帮忙吗?
答案 0 :(得分:2)
问题正是你所说的。尝试这样做:
header('Content-Type: application/json');
$arr = array();
while ($rows = $stmt->fetch(PDO::FETCH_ASSOC)) {
$autor = $rows['Nome'];
$publication = $rows['publication'];
$my = array(
"autor"=>"$autor",
"publication"=>"$publication"
);
$arr[] = $my;
}
echo json_encode($arr);
答案 1 :(得分:1)
看起来你正在使用each()函数错误。
$(data).each(function(i, value) {
alert(value.autor + " : " + value.publication)
});