0

I have a script that post a variable and get a JSON as result. This is the script:

$.post('<?php echo site_url('Admin/get_memo_by_surat'); ?>',{data:id},function(result){
   alert(result);
});

When I do alert if the script success post the variable, I get this JSON :

[{"no_dokumen":"19\/0001-1\/SCG","dari":"PPC","pic":"77973035","kepada":"PPC","instruksi":"Mas Dwi tolong hps","tanggal_dist":"2017-03-27 13:31:53","tanggal_proses":"0000-00-00 00:00:00"},{"no_dokumen":"19\/0002-1\/SCG","dari":"PPC","pic":"77973035","kepada":"PMD","instruksi":"Test to PMD","tanggal_dist":"2017-03-27 13:32:15","tanggal_proses":"0000-00-00 00:00:00"}]

I've tried using $.each, but it show nothing.

$.each(result, function(i,response){ alert(response.no_dokumen); });

So, how do I loop through this? I'd like to represent this data in a table by the way.

8
  • it should work can you create a demo? Commented Mar 27, 2017 at 7:00
  • It looks good and works for me: if I declare result equal to this JSON object and then use your $.each. Just make sure that you deserialize JSON into object before trying to iterate through it. Commented Mar 27, 2017 at 7:00
  • @guradio sorry, I have limited acces to this site only right now. Can't create demo. Commented Mar 27, 2017 at 7:02
  • @ashura91 you can see the button <> create the demo Commented Mar 27, 2017 at 7:02
  • 1
    If your JSON is a string you should call JSON.parse(yourJsonString) Commented Mar 27, 2017 at 8:03

1 Answer 1

1

this result variable contain json data but string. you just add the new line:

var data = JSON.parse(result);

and then try,

$.each(data, function(i, response){ alert(response.no_dokumen); });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.