简体   繁体   English

如何使用ajax(获取方法)在html页面(客户端)中显示来自node.js服务器的json数据?

[英]how to display json data from node.js server in html page(client) using ajax(Get Method)?

I am using node js server ,from this url : http://localhost:5000/listproducts I have the following data: 我正在使用Node JS服务器,从以下URL: http:// localhost:5000 / listproducts我有以下数据:

[{"Id":1,"Name":"New Product","Description":"P1 desc","Quantity":1},{"Id":2,"Name":"Product2","Description":"p2 desc","Quantity":7}] [{“ Id”:1,“名称”:“新产品”,“描述”:“ P1 desc”,“数量”:1},{“ Id”:2,“名称”:“ Product2”,“描述“:” p2 desc“,”数量“:7}]

i want to display the data in html page using ajax . 我想使用ajax在html页面中显示数据。

I have tried this in my html page : 我已经在我的html页面中尝试过此操作:

$('#display').click(function() {
      $.ajax({
          type: 'GET',
          url: 'http://localhost:5000/listproducts',
          dataType:"json", //to parse string into JSON object,
          success: function(data){ 
              if(data){
                  var len = data.length;
                  var txt = "";
                  if(len > 0){
                      for(var i=0;i<len;i++){
                          if(data[i].Name && data[i].Description){
                              txt += "<tr><td>"+data[i].Name+"</td><td>"+data[i].Description+"</td><td>"+data[i].Quantity+"</td></tr>";
                          }
                      }
                      if(txt != ""){
                          $("#table").append(txt).removeClass("hidden");
                      }
                  }
              }
          },
          error: function(jqXHR, textStatus, errorThrown){
              alert('error: ' + textStatus + ': ' + errorThrown);
          }
      });
      return false;
    });

but when i try it nothing happen and html console page doesnot show any errors: 但是当我尝试它没有任何反应,并且html控制台页面未显示任何错误:

html : html:

<button id="display">Display Products</button>
    <table id="table" class="hidden" style="display:none;">
        <tr>
            <th>Product</th>
            <th>Price</th>
            <th>Description</th>
            <th>Image</th> 
        </tr>
    </table>

i expect the output to be table holding the data of the products 我希望输出是保存产品数据的表

You can try like this: 您可以这样尝试:

Javascript: 使用Javascript:

$('#display').click(function() {
      $.ajax({
          type: 'GET',
          url: 'http://localhost:5000/listproducts',
          dataType:"json", //to parse string into JSON object,
          success: function(data) { 
              if(data) {
                for(let i=0;i<data.length;i++) {
                    $('#table tbody').append("<tr><td>"+data[i].Name+"</td><td>"+data[i].Description+"</td><td>"+data[i].Quantity+"</td></tr>");
                }
            }
          },
          error: function(jqXHR, textStatus, errorThrown){
              alert('error: ' + textStatus + ': ' + errorThrown);
          }
      });

      return false;
});

The HTML: HTML:

<button id="display">Display Products</button>
<table id="table" class="hidden" style="display:none;">
    <thead>
        <tr>
            <th>Product</th>
            <th>Price</th>
            <th>Description</th>
            <th>Image</th> 
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM