简体   繁体   中英

I can't extract JSON array from AJAX request, some return undefined or [object object]

I have mechanism that use AJAX to send-receive data. The data is sent to a php file then when complete the result will be sent back in JSON-from (use encode_json method). Then the json data that received will be extracted for each aim.

The problem appear, when I try to extract the json message that transfered from php file. This is the Javascript part

function set_filter() {
var a = document.getElementById('Tema');
var b = a.options[a.options.selectedIndex].value;
$.get("filter.php", {tema: b, method: 'set_filter'}, function(data, status) {
    alert(status);
    console.log(data);
    x = JSON.parse(data);
    alert(x.count);
  });
}

This How result of console.log(data)

Blockquote {"filters":{"id_filter":["2","1","3"],"judul_filter":["Jenis Arus Dana","Instansi NAD","Kategori Transaksi NAD"]},"select_list":[[["s","p"],["Sumber","Pengeluaran"]],[["1","2","3","4","5","6"],["Bank Sentral","Perbankan","Pemerintah","Domestik Lain","Luar Negeri","Seluruh Instansi"]],[["0200","0300","0400","0500","0600","0700","0800","0900","1000","1010","1020","1021","1022","1023","1024","1025","1030","1100","1200","1210","1220","1230","1300","1400","1410","1420","1500","1800","2000","9000","0100"],["Investasi Non Finansial","Pinjaman Neto","Selisih Statistik","Investasi Finansial Neto","Jumlah Penggunaan Finansial","Jumlah Sumber Finansial","Cadangan Valas Pemerintah","Klaim Dalam Valas Lainnya","Uang Dan Simpanan","Uang Dan Simpanan Dalam Valas","Uang Dan Simpanan Dalam Rupiah","Uang Kertas Dan Logam","Giro","Tabungan","Deposito Berjangka","Simpanan Rupiah Lainnya","Tabungan Giro Pos Dan Koperasi","Surat Berharga Jangka Pendek","Kredit","Kredit Bank Dalam Rupiah","Kredit Institusi Lain D.Rupiah","Kredit Dalam Valas","Modal Saham Dan Penyertaan","Surat Berharga Jangka Panjang","Surat Berharga Pemerintah","Surat Berharga Lainnya","Cadangan Asuransi Dan Pensiun","Kredit Dagang","Rekening Antar Bank","Rupa-Rupa","Tabungan Bruto"]]]}

And when I check this line "alert(x.count);", it's return

Undefined

If I want to extract all judul_filter or id_filter or other, How I can achieve each/specific element ?

NB: I've visit some link such as Cant read JSON produced by PHP into my javascript Parse JSON in JavaScript? Convert JSON string to Javascript array But from suggestion I try, it returns undefined or [pbject object]. Must the data parse to string using JSON.Stringify(data)? Or How?

-Thanks-

Looks like you are returning a json data, so use the automatic parsing of $.ajax().

You can either use $.getJSON() or pass the data type as 'json' to $.get()

function set_filter() {
    var a = document.getElementById('Tema');
    var b = a.options[a.options.selectedIndex].value;
    $.getJSON("filter.php", {
        tema: b,
        method: 'set_filter'
    }, function (data, status) {
        var judul_filter = data.filters.judul_filter;
        var id_filter = data.filters.id_filter;
        console.log(judul_filter, id_filter);

        var select_list = data.select_list;
        console.log(data.select_list.length)
    });
}

Also there is no property called count in the json, if you want to get the length of an array like select_list use data.select_list.length

您没有计数,需要使用x.length

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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