我嘗試獲取數組中的對象數量,但未定義「長度」。無法讀取ajax中未定義的屬性「長度」
我的JS
..
success: function(data){
console.log(data.item.length);
},
我的PHP
<?php
$arr = array();
$arr["item"]["book"] = "harry porter";
$arr["item"]["available"] = true;
echo json_encode($arr);
?>
輸出
{"item":{"book":"harry porter","available":true}}
什麼'dataType'你的要求T'看起來數據沒有被jQuery解析。解析後,控制檯將顯示'undefined',因爲數組具有'length'屬性而不是對象。 – undefined
在你的ajax請求中,如果你添加dataType:「json」那麼它應該工作 – Ekim