2

I worked on this for hours and I wasn't able to find a solution on your site. I have a jsonTable.php who connect to database and return a Json by echo:

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

The JQuery code I use is simple, it's:

$.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);
    }
});

The problem is it's doesn't work and return the error:

Uncaught TypeError: Cannot use 'in' operator to search for '179' in {"livres":[
{"titre":"John", "auteur":"Doe", "annee":"1989"},
{"titre":"Anna", "auteur":"Smith", "annee":"1989"},
{"titre":"Peter", "auteur":"Jones", "annee":"1989"}
]}

Strangely I didn't find that much example and I can solve it by my self.

2
  • 1
    Provide the code for jsonTable.php Commented Feb 4, 2016 at 14:04
  • response = $.parseJSON(response); Commented Feb 4, 2016 at 14:06

2 Answers 2

3

response does not contain the array you want to loop over, response.livres does.

So you probably just need to change it to:

$.each(response.livres, function (item) {
               ^^^^^^^ here
    ...
Sign up to request clarification or add additional context in comments.

4 Comments

I made the change and now I have a: Uncaught TypeError: Cannot read property 'length' of undefined. Error. I don't use lenght in the $.each. Could you tell me please.
@user3162862 What is the exact output of console.log(response);?
Uncaught TypeError: Cannot read property 'length' of undefined
Ok now my table is populated with the response=$.parseJSON(response). The problem is that all field are undefined. Rank Content UID undefined undefined undefined undefined undefined undefined undefined undefined undefined
0

here is the jsonTable.php file:

<?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);        
}
?>

The index.php has:

$.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);
    }
});

I receive in the console the error: Uncaught TypeError: Cannot read property 'length' of undefined This come from the $.each but I don't know what!!

3 Comments

You should add this to your question and not as an answer. Note that now you are overwriting your array with a string before you encode it and that will mess-up your output so you need to remove that first.
How can I get the format of a question because in comment I can barely write code.
I found the answer. This is to add a id into the function of the $.each. As:$.each(response.livres, function (id,item) {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.