I'm experimenting with using getJSON to retrieve the most recent data from my database. So far, I've stored it in an array and used json_encode(the array)
. This method successfully displays the information on the view, but the problem lies in the ajax function not detecting it. I might be overlooking something silly.
Controller:
public function insertJSON()
{
$this->load->model("values");
$queryresults = $this->values->getDb();
$arr = array();
foreach($queryresults as $row)
{
$arr[] = $row->postcode;
}
$data['arr'] = $arr;
echo json_encode($arr);
$this->load->view('answer', $data);
}
View:
<html>
<head>
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script>
<script>
$.post('/localhost/codeigniter/index.php/welcome/insertJSON', function(data) {
alert(data);
});
</script>
</script>
</head>
<body>
</body>
</html>
Var Dump of the $arr variable:
array(4) {
[0]=>
string(5) "test1"
[1]=>
string(5) "test2."
[2]=>
string(5) "test3"
[3]=>
string(5) "test4"
}