Hello, I'm having difficulty displaying the data returned by the controller via ajax call
My js is this:
var requestList = $.ajax({
type:'GET',
data:null,
url:"index.php/Pages/loadComentarios"
});
requestList.done(function(e) {
var table;
table = e;
for (var key in e) {
console.log({
key: key,
value: e[key]
});
}
$('#comentario').html(table);
});
This is the controller (ignore the query to bd in the controller, I will move to the model when I can display the data in the view)
<?php
header('Access-Control-Allow-Origin: *');
class Pages extends CI_Controller {
public function index(){
$this->load->view("template/header");
$this->load->view("template/content");
$this->load->view("template/footer");
}
public function loadComentarios(){
$data = $this->db->get("comentario")->result_array(); //colocar na model depois
$dados = array('comentarios' => $data);
//print_r($dados) ;
echo json_encode($dados);
}
}
this is my view
<div id="comentario">
<p><b>Autor:</b> José</p>
<p><b>Data:</b> 09/04/2018 </p>
<p><b>Comentário:</b> Lorem Ipsum dolor at</p>
</div>
This is how the data is returned to me
{"comentarios":[{"id":"1","id_autor":"1","comentario":"Lorem Ipsum Dolor at Pae Dum","criacao":"2018-04-17"},{"id":"3","id_autor":"1","comentario":"Teste de Coment\u00e1rio","criacao":"2018-04-01"},{"id":"4","id_autor":"2","comentario":"Teste novo de Coment\u00e1rio sem Autor","criacao":"2018-04-09"},{"id":"5","id_autor":"2","comentario":"ao comentario ehnois","criacao":"2018-04-11"}]}
I have tried to return the data in json and normal array, but I can not access the indices to display as I want, I am using codeigniter, I am learning mvc now
If someone can help me, I appreciate it!