【问题标题】:Ajax read JSON - Cannot read property 'name' of undefinedAjax 读取 JSON - 无法读取未定义的属性“名称”
【发布时间】:2018-08-31 18:22:06
【问题描述】:

我的 report.php 返回一个 json 文件。这是我尝试阅读的 javascript:

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
$(document).ready(function () {
        var data;
        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: 'report.php',
            data: data,
            success: function (data) {
                console.log(data[0].name);
            }
        });
    });
</script>

JSON

{
    "report": {
        "type": "realtime",
        "elements": [{
            "id": "datetime",
            "name": "Date"
        }, {
            "id": "page",
            "name": "Page"
        }],
        "reportSuite": {
            "id": "myID",
            "name": "GD"
        },
        "period": "2018-08-31T08:31:26+0100\/2018-08-31T10:31:26+0100",
        "metrics": [{
            "id": "instances",
            "name": "Instances",
            "type": "number",
            "decimals": 0
        }],
        "data": [{
            "name": "2018-08-31T08:31:26+0100",
            "year": 2018,
            "month": 8,
            "day": 31,
            "hour": 8,
            "minute": 31,
            "breakdown": [{
                "name": ":A",
                "trend": "-91933.00",
                "counts": ["946801"]
            }, {
                "name": ":A Overview",
                "trend": "-97580.00",
                "counts": ["692229"]
            }, {
                "name": ":Successfull",
                "trend": "-39664.00",
                "counts": ["587378"]
            }, {
                "name": ":Trans",
                "trend": "-64227.00",
                "counts": ["440308"]
            }, {
                "name": ":Login",
                "trend": "-21233.00",
                "counts": ["367356"]
            }, {
                "name": ":Login - Enter Passcode",
                "trend": "-20456.00",
                "counts": ["334372"]
            }, {
                "name": ":Login - Complete",
                "trend": "-20724.00",
                "counts": ["321480"]
            }, {
                "name": ":Logiln - Complete",
                "trend": "-19448.00",
                "counts": ["291264"]
            }, {
                "name": "Li6",
                "trend": "-6278.00",
                "counts": ["100971"]
            }],
            "breakdownTotal": ["7118755"]
        }, {
            "name": "2018-08-31T09:31:26+0100",
            "year": 2018,
            "month": 8,
            "day": 31,
            "hour": 9,
            "minute": 31,
            "breakdown": [{
                "name": ":Accounts",
                "trend": "-91933.00",
                "counts": ["854868"]
            }, {
                "name": ":Overview",
                "trend": "-97580.00",
                "counts": ["594649"]
            }, {
                "name": "Li6",
                "trend": "-6278.00",
                "counts": ["94693"]
            }],
            "breakdownTotal": ["6613117"]
        }],
        "totals": ["13731872"],
        "version": "1.4.17.2"
    }
}

不是在控制台中打印 json,而是给我这个错误:

Uncaught TypeError: Cannot read property 'name' of undefined
    at Object.success (index.php:23)
    at c (jquery-1.9.1.min.js:3)
    at Object.fireWith [as resolveWith] (jquery-1.9.1.min.js:3)
    at k (jquery-1.9.1.min.js:5)
    at XMLHttpRequest.r (jquery-1.9.1.min.js:5)

谁能帮我理解为什么?

【问题讨论】:

  • 您需要哪个元素的名称?
  • data varable 在我看来就像一个对象,所以我猜data[0] 将是未定义的?
  • 你的数据只是一个对象使用data.report.data[0].name

标签: javascript json ajax


【解决方案1】:
console.log(data.report.data[0].breakdown[1].name);

这将输出:A Overview

其中 0 是数据数组的第一个元素,1 是细分数组的第二个元素。也许您想使用.each() 循环遍历数组?

https://api.jquery.com/jQuery.each/

【讨论】:

    【解决方案2】:

    首先不要隐藏变量。
    将您的数据重命名为 result(或类似名称)。
    其次,在您的 JSON 中,您必须选择 json 的数据类型。 使用result.report.data[0].name

    【讨论】:

      【解决方案3】:

      查看 JSON,我认为您缺少访问“报告”对象,因此您想要获取第一个数据元素的名称是:

      data.report.data[0].name
      

      而不仅仅是

      data[0].name
      

      【讨论】:

        【解决方案4】:

        应该是这样的

        console.log(data.report.elements[0].name);
        

        【讨论】:

          【解决方案5】:

          变量data 保存着整个响应对象。它不是一个数组。你为什么不试试这个方法。

          data.report.elements[0].name

          【讨论】:

            猜你喜欢
            • 2021-12-06
            • 2022-01-22
            • 2021-11-13
            • 2022-01-14
            • 2022-06-22
            • 1970-01-01
            • 2021-12-13
            • 1970-01-01
            相关资源
            最近更新 更多