0

I need help on how to create table from data fetch from API server in html.

A possible duplicate of this question

I need to request json data from a url, then use that data to dynamically create table in html.

Below are my test.html

<!DOCTYPE html>
<html>
<head>
...
...
</head>
<body>
...
...
...
<div class="row">
    <div class="col-xs-12">
        <div class="box">
            <div class="box-header">
              <h3 class="box-title">Activity</h3>
            </div>
            <div id = "showData" class="box-body"></div>
        </div>
     </div>
 </div>

<script src="../static/plugins/jQuery/jquery-2.2.3.min.js"></script>
<script src="../static/plugins/jQueryUI/jquery-ui.min.js"></script>

<script>
  $.ajax({
    url: "http://127.0.0.1:4220/pipeline/v1/bpwi/",
    success: function (data) {
      var col = [];
      for (var i = 0; i < data.length; i++) {
        for (var key in data[i]) {
          if (col.indexOf(key) === -1) {
            col.push(key);
          }
        }
      }

      var table = document.createElement("table");

      var tr = table.insertRow(-1);

      for (var i = 0; i < col.length; i++) {
        var th = document.createElement("th");
        th.innerHTML = col[i];
        tr.appendChild(th);
      }

      for (var i = 0; i < data.length; i++) {
        tr = table.insertRow(-1);
        for (var j = 0; j < col.length; j++) {
          var tabCell = tr.insertCell(-1);
          tabCell.innerHTML = myBooks[i][col[j]];
        }
      }

      var divContainer = document.getElementById("showData");
      divContainer.innerHTML = "";
      divContainer.appendChild(table);
    }
  })
</script>
</body>
</html>

Expected ajax response is something like this

[
        {
            "ID": "1",
            "Name": "Computer",
            "Category": "Computers",
            "Price": "125.60"
        },
        {
            "ID": "2",
            "Name": "Blue",
            "Category": "Sport",
            "Price": "56.00"
        },
        {
            "ID": "3",
            "Book Name": "Science",
            "Category": "Science",
            "Price": "210.40"
        }
    ]

I have refreshed my browser many times but the result is not good.

Here is the result

-----------------------------------------------------------------
Activity
-----------------------------------------------------------------
0
[
{
"
I
D
"
:
"
1
"
...
...

I am really new to javascript and some of good example I found is the possible question I posted above and here but both did not show a clear example on how to implement http request for getting the data from external source.

I really hope someone can pointed out for me what I did wrong in this code. Thank you for your help and suggestion.

2
  • 1
    why didnt you define in ajax parameters dataType and type? Commented Nov 8, 2017 at 10:29
  • right!!...thank you. Your suggestion helped me Commented Nov 8, 2017 at 11:05

0

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.