【问题标题】:Display JSON data HTML - Undefined显示 JSON 数据 HTML - 未定义
【发布时间】:2020-09-02 10:13:55
【问题描述】:

大家好,我是编程新手。我想在 HTML 表 jquery 中显示 json 数据。我从服务器收到的输出是:未定义。我想在页面上显示带有事件的表格。事件列表将在 json 文件中不断更新。

HTML

<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
</head>
<body>
    <div class="container">
    <div class="table-responsive">
        <table class="table table-bordered table-stripped" id="events_table">
            <tr>
                <th>Title</th>
                <th>Venue</th>
                <th>Date</th>
                <th>Description</th>
                <th>URL</th>
            </tr>
        </table>
        </div>
    </div>
</body>

jQuery

<script>
$(document).ready(function(){
    $.getJSON("events.json", function(data){
        var events_data = '';
        $.each(data, function(key, value){
            events_data += '<tr>';
            events_data += '<td>'+value.title+'</td>';
            events_data += '<td>'+value.venue+'</td>';
            events_data += '<td>'+value.start_date+'</td>';
            events_data += '<td>'+value.html_description+'</td>';
            events_data += '<td>'+value.sharing_url+'</td>';
            events_data += '<tr>';
        });
        $('#events_table').append(events_data);
    });
});

示例 json

{
  "events": [
    {
      "id": 40818,
      "title": "Customer Service",
      "venue": "online",
      "location": null,
      "contact_email": "demo@demo.com",
      "contact_phone_number": "002",
      "public": true,
      "created_at": "2020-09-02T06:52:05Z",
      "start_date": "2020-09-15T00:00:00Z",
      "end_date": "2020-09-15T01:00:00Z",
      "html_description": "<h3><span style=\"background-color:null;\">Customer Service can always be a bit of a mine-field and something that is all too often an afterthought for some businesses.&nbsp;</span></h3>\r\n\r\n<p>This should not be the case!&nbsp;Serving your customers better and putting them at the heart of your business model is key to your success.&nbsp;Join me.</p>\r\n",
      "attendees_count": 1,
      "status": "published",
      "categories": [
        "webinar"
      ],
      "sharing_url": "https://google.com"
    },
}

【问题讨论】:

  • 如果“您从服务器收到的输出是:未定义”,您需要弄清楚该端点和/或服务器出了什么问题。
  • @goto 不是,OP 正在循环响应的错误“字段”

标签: javascript html jquery json


【解决方案1】:

您好,请查看此链接 -> Click me

                var data = {
  "events": [
    {
      "id": 40818,
      "title": "Customer Service",
      "venue": "online",
      "location": null,
      "contact_email": "demo@demo.com",
      "contact_phone_number": "002",
      "public": true,
      "created_at": "2020-09-02T06:52:05Z",
      "start_date": "2020-09-15T00:00:00Z",
      "end_date": "2020-09-15T01:00:00Z",
      "html_description": "<h3><span style=\"background-color:null;\">Customer Service can always be a bit of a mine-field and something that is all too often an afterthought for some businesses.&nbsp;</span></h3>\r\n\r\n<p>This should not be the case!&nbsp;Serving your customers better and putting them at the heart of your business model is key to your success.&nbsp;Join me.</p>\r\n",
      "attendees_count": 1,
      "status": "published",
      "categories": [
        "webinar"
      ],
      "sharing_url": "https://google.com"
    },
    {
      "id": 40819,
      "title": "Customer Service 2",
      "venue": "online",
      "location": null,
      "contact_email": "demo@demo.com",
      "contact_phone_number": "002",
      "public": true,
      "created_at": "2020-09-02T06:52:05Z",
      "start_date": "2020-09-15T00:00:00Z",
      "end_date": "2020-09-15T01:00:00Z",
      "html_description": "<h3><span style=\"background-color:null;\">Customer Service can always be a bit of a mine-field and something that is all too often an afterthought for some businesses.&nbsp;</span></h3>\r\n\r\n<p>This should not be the case!&nbsp;Serving your customers better and putting them at the heart of your business model is key to your success.&nbsp;Join me.</p>\r\n",
      "attendees_count": 1,
      "status": "published",
      "categories": [
        "webinar"
      ],
      "sharing_url": "https://google.com"
    },
]
};
        var events_data = [];
        $.each(data.events, function(key, value){
            events_data.push('<tr>');
            events_data.push('<td>'+value.title+'</td>');
            events_data.push('<td>'+value.venue+'</td>');
            events_data.push('<td>'+value.start_date+'</td>');
            events_data.push('<td>'+value.html_description+'</td>');
            events_data.push('<td>'+value.sharing_url+'</td>');
            events_data.push('<tr>');
        });
        $('#events_table').append(events_data.join(" "));
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
</head>
<body>
    <div class="container">
    <div class="table-responsive">
        <table class="table table-bordered table-stripped" id="events_table">
            <tr>
                <th>Title</th>
                <th>Venue</th>
                <th>Date</th>
                <th>Description</th>
                <th>URL</th>
            </tr>
        </table>
        </div>
    </div>
</body>

【讨论】:

    【解决方案2】:

    您需要遍历data.events 而不仅仅是data

    $.each(data.events, function(key, value) {
    

    更新了sn-p

    //$.getJSON("events.json", function(data){
    var data = {
      "events": [{
        "id": 40818,
        "title": "Customer Service",
        "venue": "online",
        "location": null,
        "contact_email": "demo@demo.com",
        "contact_phone_number": "002",
        "public": true,
        "created_at": "2020-09-02T06:52:05Z",
        "start_date": "2020-09-15T00:00:00Z",
        "end_date": "2020-09-15T01:00:00Z",
        "html_description": "<h3><span style=\"background-color:null;\">Customer Service can always be a bit of a mine-field and something that is all too often an afterthought for some businesses.&nbsp;</span></h3>\r\n\r\n<p>This should not be the case!&nbsp;Serving your customers better and putting them at the heart of your business model is key to your success.&nbsp;Join me.</p>\r\n",
        "attendees_count": 1,
        "status": "published",
        "categories": [
          "webinar"
        ],
        "sharing_url": "https://google.com"
      }]
    };
    
    var events_data = '';
    $.each(data.events, function(key, value) {
      events_data += '<tr>';
      events_data += '<td>' + value.title + '</td>';
      events_data += '<td>' + value.venue + '</td>';
      events_data += '<td>' + value.start_date + '</td>';
      events_data += '<td>' + value.html_description + '</td>';
      events_data += '<td>' + value.sharing_url + '</td>';
      events_data += '<tr>';
    });
    $('#events_table').append(events_data);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    <div class="container">
      <div class="table-responsive">
        <table class="table table-bordered table-stripped" id="events_table">
          <tr>
            <th>Title</th>
            <th>Venue</th>
            <th>Date</th>
            <th>Description</th>
            <th>URL</th>
          </tr>
        </table>
      </div>
    </div>

    【讨论】:

      猜你喜欢
      • 2018-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-30
      • 2021-06-23
      • 2012-04-20
      • 2017-11-07
      相关资源
      最近更新 更多