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);
 });
});
asked Oct 26, 2012 at 10:34
1

1 Answer 1

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);
 });
});
answered Oct 26, 2012 at 10:37

1 Comment

thanks @Svetlio - simple but effective!! :) will accept shortly

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.