-2

My AJAX response contains the elements of a database. I have a table on my page #people and as the AJAX request succeeds, I need to populate my table with the database records. This is the code I am using (that is a <script> in my index.html):

$(document).ready(function(){
    $.ajax({
            type: "GET",
            url: "res/main.php",
            data: { command : "loadPeople" },
            success: function( resp ){
                for(var i = 1; i<= people.length; i++){
                    var tableRow = "<tr><td>" + people[i].id + "</td><td>" + people[i].name + "</td></tr>";
                    $("#people > tbody:last").append(tableRow);
                }
                console.log(resp.people);
            }
    });
});

(the code I am using to fill the table is taken from this StackOverflow question)

I am using FireBug in Chrome, and when I refresh the page, nothing appears in its console, and my table is unchanged. What can I do to solve this error? Where is the error?

EDIT:

I renamed resp to people, now my table is being populated with about one thousand items labeled undefined.

This is a sample of my people:

[{"id":"1","name":"Victor "},{"id":"2","name":"Dan "},{"id":"3","name":"John"},{"id":"4","name":"Mady"},{"id":"5","name":"Mary"},{"id":"6","name":"Michael"},{"id":"7","name":"Michaela"}]

(encoded in PHP)

Community
  • 1
  • 1
Victor
  • 13,914
  • 19
  • 78
  • 147

2 Answers2

0

try change

 success: function( resp ){

with

 success: function( people){
Kanishka Panamaldeniya
  • 17,302
  • 31
  • 123
  • 193
0

I found where my problem was. Inside the $.ajax request, I had to specify dataType : "json". Now my table is correctly populated, and the console looks good.

Victor
  • 13,914
  • 19
  • 78
  • 147