0

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-

Community
  • 1
  • 1
m hanif f
  • 406
  • 1
  • 7
  • 20
  • you do not have `count` in your JSON. If you want to know the count of your object use `x.length` – Javad Mar 17 '14 at 02:30

2 Answers2

0

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

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0

You do not have count, you need use x.length