简体   繁体   English

如何从 AJAX jquery 函数访问多维关联 json 数组

[英]How to access multidimensional associative json arrays from AJAX jquery function

I have to return a return a multidimensional associative arrays from PHP page for a jQuery AJAX function.我必须从 PHP 页面为 jQuery AJAX 函数返回一个多维关联数组。

This is my PHP page called 'seachsongs.php'这是我的 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));

    ?>

This is my jQuery AJAX function这是我的 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');
            }

        });

    });

});

How can I access to array data from jQuery?如何从 jQuery 访问数组数据? What is the correct statement?正确的说法是什么?

alert(data)

print this打印这个

{"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: sorry form my bad english. PS:对不起,我的英语不好。

I've set the data type of this request to be json , so the response returned should be a json object.我已将此请求的数据类型设置为json ,因此返回的响应应为json对象。 You can access it by this way:您可以通过以下方式访问它:

$(document).ready(function () {
    $('#nBrano').keyup(function () {
        nomeBrano = $(this).val();
        $.ajax({
            type: "POST",
            data: {brano: nomeBrano},
            url: "searchsong.php",
            dataType:'json',
            success: function (response) {
                for(var i=0; i<response['data'].length; i++){
                    console.log(response['data'][i][/*your_target_index*/]);
                }
            },
            error: function () {
                //alert('errore');
            }
        });
    });
});

see more about ajax查看更多关于 ajax

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM