0

I've created a controller & view as follows - I am trying to loop through the jSON object - can anyone assist?

Can anyone demonstrate how to loop through the json object in the view?

//Controller PHP function:
public function ajax_get_all()
{
    $stockists = $this->stockists_model->get_all();
    header('Content-type: application/json');
    echo json_encode($stockists);
}

//HTML View

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">
$(function() {
// Stuff to do as soon as the DOM is ready;
$.getJSON("/stockists/ajax_get_all", function(data) {
    var obj = jQuery.parseJSON(data);
    //console.log(obj);
  });
});

Zabs
  • 13,852
  • 45
  • 173
  • 297
  • possible duplicate : http://stackoverflow.com/questions/2342371/jquery-loop-on-json-data-using-each – Techie Oct 26 '12 at 10:41

1 Answers1

2
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">
$(function() {
// Stuff to do as soon as the DOM is ready;
$.getJSON("/stockists/ajax_get_all", function(data) {
    $.each(data, function(key,value){ 
      console.log(key);
      console.log(value);  
    });
    //var obj = jQuery.parseJSON(data);
    //console.log(obj);
  });
});
Svetoslav
  • 4,686
  • 2
  • 28
  • 43