【问题标题】:Ajax JQuery request to Mysql with php doesn't work with JSON使用 php 对 Mysql 的 Ajax JQuery 请求不适用于 JSON
【发布时间】:2016-05-14 04:29:32
【问题描述】:

我为此工作了几个小时,但无法在您的网站上找到解决方案。我有一个连接到数据库并通过回显返回 Json 的 jsonTable.php:

{
    "livres": [{ 
        "titre": "John", 
        "auteur": "Doe", 
        "annee": "1989"
    },{ 
        "titre": "Anna", 
        "auteur": "Smith", 
        "annee": "1989"
    },{ 
        "titre": "Peter", 
        "auteur": "Jones", 
        "annee": "1989"
    }]
}

我使用的jQuery代码很简单,就是:

$.ajax({
    url: 'jsonTable.php',
    type: 'GET',
    dataType : 'json',
    /*data: {
        json: jsonData
    },*/
    success: function (response) {
        alert(response);
        console.log(response);
        var trHTML = '';
        $.each(response, function (item) {
            trHTML += '<tr><td>' + item.titre + '</td><td>' + item.auteur + '</td><td>' + item.annee + '</td></tr>';
        });
        $('#records_table').append(trHTML);
    }
});

问题是它不起作用并返回错误:

未捕获的类型错误:无法使用 'in' 运算符在 {"livres":[
中搜索 '179' {"tire":"John", "auteur":"Doe", "annee":"1989"},
{"titre":"Anna", "auteur":"Smith", "annee":"1989"},
{“标题”:“彼得”,“作者”:“琼斯”,“安妮”:“1989”}
]}

奇怪的是我没有找到那么多例子,我可以自己解决。

【问题讨论】:

  • 提供jsonTable.php的代码
  • response = $.parseJSON(response);

标签: php jquery mysql json ajax


【解决方案1】:

response 不包含您要循环的数组,response.livres 包含。

所以您可能只需要将其更改为:

$.each(response.livres, function (item) {
               ^^^^^^^ here
    ...

【讨论】:

  • 我进行了更改,现在我有一个:未捕获的类型错误:无法读取未定义的属性“长度”。错误。我不在 $.each 中使用长度。请告诉我。
  • @user3162862 console.log(response); 的确切输出是什么?
  • 未捕获的类型错误:无法读取未定义的属性“长度”
  • 好的,现在我的表填充了 response=$.parseJSON(response)。问题是所有字段都未定义。排名 内容 UID undefined undefined undefined undefined undefined undefined undefined undefined undefined
【解决方案2】:

这里是 jsonTable.php 文件:

<?php




$dbhote = "localhost";
$dbutilisateur = "";
$dbpasse = "";
$dbnom = "";

$erreur = false;

//Connexion  MySQL Server

try {
    $connexion = new
            PDO('mysql:host=' . $dbhote . ';dbname=' . $dbnom, $dbutilisateur, $dbpasse);
} catch (Exception $e) {
    echo 'Erreur : ' . $e->getMessage() . '<br />';
    echo 'Num : ' . $e->getCode();
    $erreur = true;
}

if (!$erreur) {



    $query = "SELECT * FROM livres WHERE 1"; //WHERE sexe = '$sexe'";


    $req_prepare=$connexion->prepare($query);
    $req_prepare->execute();

   $encode = array();


   while( $ligne = $req_prepare->fetch(PDO::FETCH_ASSOC) ) {
    $encode[] = $ligne;        
    }    
    $req_prepare->closeCursor();


   // echo json_encode($encode);
    $encode = '{"livres":[
    {"titre":"John", "auteur":"Doe", "annee":"1989"},
    {"titre":"Anna", "auteur":"Smith", "annee":"1989"},
    {"titre":"Peter", "auteur":"Jones", "annee":"1989"}
]}';
    echo json_encode($encode);        
}
?>

index.php 有:

$.ajax({
    url: 'jsonTable.php',
    type: 'GET',
    dataType : 'json',
    /*data: {
        json: jsonData
    },*/
    success: function (response) {
        alert(response);
        console.log(response);
        var trHTML = '';
        $.each(response.livres, function (item) {
            trHTML += '<tr><td>' + item.titre + '</td><td>' + item.auteur + '</td><td>' + item.annee + '</td></tr>';
        });
        $('#records_table').append(trHTML);
    }
});

我在控制台中收到错误: 未捕获的类型错误:无法读取未定义的属性“长度” 这来自 $.each 但我不知道是什么!!

【讨论】:

  • 您应该将此添加到您的问题中,而不是作为答案。请注意,现在您在编码之前用字符串覆盖数组,这会弄乱您的输出,因此您需要先将其删除。
  • 如何获得问题的格式,因为在评论中我几乎无法编写代码。
  • 我找到了答案。这是在 $.each 的函数中添加一个 id。如:$.each(response.livres, function (id,item) {
猜你喜欢
  • 1970-01-01
  • 2021-04-25
  • 2014-09-09
  • 2019-08-18
  • 2012-08-29
  • 1970-01-01
  • 1970-01-01
  • 2015-08-24
  • 1970-01-01
相关资源
最近更新 更多