jQuery没有警报数组数据

时间:2015-08-23 17:11:39

标签: javascript php jquery pdo

结果中的数组;

 {"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);      
    }

的Javascript;

            dataType: "json",
            success: function (data) {
                     $(data).each(function(autor, pubication) {
                     alert(data.autor + " : " + data.publication) 
                     });
            }
  

我的问题:jQuery没有提醒数据

     

我认为问题是因为结果之间没有逗号,因为当只有一个结果时,他通常会在将来警告想要在数据中创建.append,但我不能简单地提醒数据,可以有人帮忙吗?

2 个答案:

答案 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) 
});