weixin_33709590 2020-02-29 10:44 采纳率: 0%
浏览 157

在HTML页面中显示Ajax

I'm using flask as a backend, and one of my endpoints is objectList, which returns data in this format:

[{name1:value1, name2:value2}, {name1:value3, name2:value4}]

In order to display this data on my html page, I'm using the following code:

<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script>
      $(document).ready(function){
        $.ajax({
          url: 'localhost:5000/objectList',
          method:"GET",
          success: function(result){
            $("#div1").html(result);
          };
        });
      };
    </script>
  </head>
  <body>

    <div id="div1"></div>
  </body>
</html>

This doesn't display any data. What am I doing wrong and how can I fix this?

Edit: just a clarification, I want the data to load when the page loads, not on a button or a click

  • 写回答

2条回答 默认 最新

  • weixin_33670786 2020-02-29 10:54
    关注

    <!DOCTYPE html>
    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
      $("button").click(function(){
        $.getJSON("demo_ajax_json.js", function(result){
          $.each(result, function(i, field){
            $("div").append(field + " ");
          });
        });
      });
    });
    </script>
    </head>
    <body>
    
    <button>Get JSON data</button>
    
    <div></div>
    
    </body>
    </html>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程