我必須從PHP頁面爲jQuery AJAX函數返回一個多維關聯數組。如何從AJAX訪問多維關聯json數組jquery函數
這就是所謂的我的PHP頁面 'seachsongs.php'
<?php
$brano = $_POST['brano'];
$sql = "SELECT Titolo, Autore FROM Brani WHERE Titolo = '$brano';";
$ris = $conn->query($sql);
while ($row = $ris->fetch_array()) {
$arr[] = $row;
}
echo json_encode(array('data' => $arr));
?>
這是我的jQuery AJAX功能
$(document).ready(function() {
$('#nBrano').keyup(function() {
nomeBrano = $(this).val();
$.ajax({
type: "POST",
data: {brano: nomeBrano},
url: "searchsong.php",
success: function (data) {
document.write(data);
//alert("Prova" + data['data'][0]["Titolo"]);
/*if (msg != 'null') {
$('#similarSongs').css('display', 'block');
/*$.each(prova, function (key1, value1) {
$.each(value1['two']['three'], function (key1, value1) {
document.write('test');
});
})
$('#similarSongs table').html(tabella);
}
if (msg == 'null') {
$('#similarSongs table').html("Nessun brano simile trovato");
$('#similarSongs').css('display', 'block');
}*/
},
error: function() {
//alert('errore');
}
});
});
});
如何從jQuery的訪問數組數據?什麼是正確的說法?
alert(data)
打印此
{"data":[{"0":"Animals","Titolo":"Animals","1":"Martin Garrix","Autore":"Martin Garrix"},{"0":"Animals","Titolo":"Animals","1":"Maron V","Autore":"Maron V"}]}{"data":[{"0":"Animals","Titolo":"Animals","1":"Martin Garrix","Autore":"Martin Garrix"},{"0":"Animals","Titolo":"Animals","1":"Maron V","Autore":"Maron V"}]}
PS:抱歉,形成我的英語不好。
你會希望你以後它'data'辦? – imudin07
這是從一個pdo中的fetchAll? – 25r43q
您可以將'dataType:'json''添加到您的Ajax調用中,然後jQuery會將它轉換爲您。 –