我有这个,数据json从laravel API创建
[
[{
"id": 4,
"name": "Customer-izlgy",
"email": "ycgjl@xxyin.com"
}, {
"id": 5,
"name": "Customer-bkxjn",
"email": "wlrew@iloiv.com"
}],
[{
"id": 1,
"name": "Customer-izlgyt",
"email": "ycgjl@xxysdin.com"
}, {
"id": 2,
"name": "Customer-bkxjnyf",
"email": "wlrew@iloifsv.com"
}]
]
我试图通过 ajax 获取数据 id 4 和 id 1,但总是出错 未捕获的类型错误:无法读取未定义的属性"名称">
这是我从上面的数据 json 中获取名称的代码。
// get customer data that has been handled
function callHandledCustomer() {
$.ajax({
type: "GET",
url: rootUrl + "/api/chat/getHandledCustomer",
dataType: "json",
success: function(response){
$.each(response, function(i, customer) {
console.log(customer[0][0].name); // get name
});
}
});
}
多谢
您只需删除与对象布局不匹配的第二个[0]
即可。
var response = [
[{
"id": 4,
"name": "Customer-izlgy",
"email": "ycgjl@xxyin.com"
}, {
"id": 5,
"name": "Customer-bkxjn",
"email": "wlrew@iloiv.com"
}],
[{
"id": 1,
"name": "Customer-izlgyt",
"email": "ycgjl@xxysdin.com"
}, {
"id": 2,
"name": "Customer-bkxjnyf",
"email": "wlrew@iloifsv.com"
}]
];
$.each(response, function(index, customer){
console.log(customer[0].name);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>