Ajax读取JSON-无法读取未定义的属性“名称" [英] Ajax read JSON - Cannot read property 'name' of undefined
本文介绍了Ajax读取JSON-无法读取未定义的属性“名称"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的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)
有人可以帮助我理解原因吗?
解决方案
console.log(data.report.data[0].breakdown[1].name);
哪个将输出:A Overview
其中0是数据数组的第一个元素,而1是细分数组的第二个元素.也许您想使用.each()
遍历数组?
https://api.jquery.com/jQuery.each/ >
My report.php returns a json file. This is my javascript to try and read it:
<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"
}
}
Instead of printing the json in console, it gives me this error:
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)
Can anyone please help me understand why?
解决方案
console.log(data.report.data[0].breakdown[1].name);
Which will output :A Overview
Where 0 is the first element of your data array, and 1 is the second element of your breakdown array. Maybe you want to loop through the array with .each()
?
https://api.jquery.com/jQuery.each/
这篇关于Ajax读取JSON-无法读取未定义的属性“名称"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文