JSON オブジェクトのプロパティにアクセスしようとしています。私のajax呼び出しは次のとおりです。
$.ajax({
url: 'login.php',
type: 'post',
dataType: 'json',
data: $('#frmLgn').serialize(),
success: function(data) {
console.log(data[0].message);
console.log(data[1].message);
console.log(data[2].message);
}
PHPは次のとおりです。
for ($i = 0; $i < $queryMsgCntResults; $i++) {
$queryGetNew = "SELECT message, msgID FROM $username WHERE isNew = 1;";
try
{
$stmt = $db->prepare($queryGetNew);
$stmt->execute();
$message = $stmt->fetch(PDO::FETCH_ASSOC);
$messageArray[] = $message;
}
catch(PDOException $ex)
{
die("Failed to run query: " . $ex->getMessage());
}
}
echo json_encode($messageArray);
}
これが出力されることを期待しています:
console.log(data[0].message); //contents of message1
console.log(data[1].message); //contents of message2
console.log(data[2].message); //contents of message3
しかし、代わりに次を取得します。
console.log(data[0].message); //contents of message1
console.log(data[1].message); //contents of message1
console.log(data[2].message); //contents of message1
私は何を見逃していますか/台無しにしていますか?
この質問を閉じてください。明確にするために、再編成し、再テストし、おそらく再投稿します。皆様のご支援に感謝いたします。時間を無駄にしてしまったことをお詫び申し上げます。ありがとうございました。