0

I have an ajax function which sends a get request to an api and retrieves some JSON, which I am displaying in a table. This is what I have tried-

<script>




function getInfo() {


     $.ajax({
         type: "GET",
         url: "http://example.com/",
         contentType: "application/json; charset=utf-8",
         crossDomain: true,
         dataType: "json",
         success: function (data, status, jqXHR) {

            for (var i=0;i<data.length;i++) {

            $("table.mytable").append("<tr><td>First Name</td><td>" + data[i].firstname + "</td></tr><tr><td>Last Name</td><td>" + data[i].lastname + 
            "</td></tr><tr><td>Queues</td><td>" + 
             data[i].skills +  "</td></tr>"  );
          }


             alert("success");
         },

         error: function (jqXHR, status) {
             // error handler
             console.log(jqXHR);
             alert('fail' + status.code);
         }
      });
   }


</script>

However, the output on the html page looks like this-

First Name John Last Name Smith Skills Maths First Name Jane Last Name Smith Skills Maths

I would like the First Name, Last Name and Skills headers to be across the table, with the data underneath. Any idea how to do this?

4
  • 1
    Yeah, try to think logical? Do you know how to make an HTML table!? Commented Nov 22, 2013 at 15:04
  • Evidently not, but I do have some manners Commented Nov 22, 2013 at 15:04
  • 1
    Suggest: First build up a hardcoded HTML table with dummy data inside, if you like the result, generalize it and use JavaScript to insert dynamic data into it. I bet you have syntax error in HTML. Commented Nov 22, 2013 at 15:05
  • Each td is the start of a new column, so, your first row should be hardcoded to be the headers, then just append your data info. Commented Nov 22, 2013 at 15:06

1 Answer 1

1

Replace you for loop with this:

$("table.mytable").html("<tr><th>First Name</th><th>Last Name</th><th>Queues</th></tr>"  );

for (var i=0;i<data.length;i++) {
   $("table.mytable").append("<tr><td>" + data[i].firstname + "</td><td>Last Name</td><td>" + data[i].lastname + "</td><td>" + data[i].skills +  "</td></tr>"  );
}

The whole ajax should look like this:

 $.ajax({
         type: "GET",
         url: "http://example.com/",
         contentType: "application/json; charset=utf-8",
         crossDomain: true,
         dataType: "json",
         success: function (data, status, jqXHR) {

            $("table.mytable").html("<tr><th>First Name</th><th>Last Name</th><th>Queues</th></tr>"  );

    for (var i=0;i<data.length;i++) {
       $("table.mytable").append("<tr><td>" + data[i].firstname + "</td><td>Last Name</td><td>" + data[i].lastname + "</td><td>" + data[i].skills +  "</td></tr>"  );
    }


             alert("success");
         },

         error: function (jqXHR, status) {
             // error handler
             console.log(jqXHR);
             alert('fail' + status.code);
         }
      });
Sign up to request clarification or add additional context in comments.

Comments

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.