早上好,我正在使用ajax呼叫,試圖回傳一個嵌套json陣列的值,我成功地拉出了資料,除了嵌套陣列 "booking_status"。當我嘗試回圈瀏覽 "booking_status "時,我得到了[object Object], [object Object], [object Object]的值。如果我試圖指定一個值,例如 item.booking_status.status,我得到這個undefined。
這是我的嵌套陣列
[
{
"id"。1,
"item_name": "Misrry",
"author": "Stephen King",
"booking_status": [
{
"check_in": "2021-09-22T08:27:00 04:00",
"check_out": "2021-09-23T08:27:00 04:00",
"status": "當前已預訂"。
},
{
"check_in": "2021-09-25T08:37:00 04:00",
"check_out": "2021-09-26T08:37:00 04:00",
"status": "當前免費"。
},
{
"check_in": "2021-09-27T08:37:00 04:00",
"check_out": "2021-09-28T08:37:00 04:00",
"status": "Current Free", "status".
}
]
{
"id": 2,
"item_name": "玫瑰紅",
"author": "Stephen King"。
"booking_status": [
{
"check_in": "2021-09-22T08:27:00 04:00",
"check_out": "2021-09-23T08:27:00 04:00",
"status": "當前已預訂"。
},
{
"check_in": "2021-09-25T08:37:00 04:00",
"check_out": "2021-09-26T08:37:00 04:00",
"status": "Current Free", "status".
}
]
}
而這是我的腳本:
<script>
$(document).ready(function () {
$.ajax({
url: 'http://localhost:8000/',
dataType: 'JSON',
success: function (data) {
for (var i = 0; i < data.length; i ) {
$(document).ready(function () {
var content = ' '
data.forEach(item => {
content = "<tr><td>" item."<td>"
item.item_name "</td> <td>"
item.author "</td><td>" item.booking_status
"</td></td></tr>"/span>
})
$('#table_body').html(內容)。
})
}
}
});
});
</script>
uj5u.com熱心網友回復:
你的代碼中有幾個錯誤。
- 你正在對
data進行兩次迭代。(for回圈和data.forEach);
洗掉for回圈,它是多余的。 booking_status是一個陣列,所以你必須對它進行字串化處理,或者以某種方式迭代它。- 你不需要嵌套
$(document).ready。內部呼叫可以被移除。
$(document).ready(function(>/span>) {
$.ajax({
url: 'http://localhost:8000/',
dataType: 'JSON',
success: function(data) {
var content = ''/span>;
data.forEach(item => {
content = "<tr>< td>"
item.item_name "</td> <td>"
item.author "</td><td>"/span> item.booking_status. map(span class="hljs-params">s => s. status).join('<br/> ' )
"</td></td></tr>"。
});
$('#table_body').html(內容)。
});
});
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/334250.html
標籤:
