1
Hello, I’m having trouble 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 bd query 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 the way 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 tried to return the data in json and normal array, but I can’t access the indices to display as I want, I’m using codeigniter, I’m learning mvc now
If anyone can help me I’d appreciate it!
In your ajax request, your "date" is null... By the way, just one piece of advice... I don’t know if you’re learning IC on your own initiative or if for a specific reason.. but personally I advise you to give Laravel a chance. :)
– Luís Almeida
It is that I do not want to send any data p/ controller, I’m using only to receive the data of the bd that it brings back, if I put the E that returns of the controller in the comments div, it appears the data all in json or normal array, but I can’t access Word by Word to leave formatted you know? = / I’m going by CI first pq gave a read and said it was the one with the shortest learning curve euaheu
– Lucas Sapienza
e[key]. nameDoCampo I don’t know CI but Laravel is very show, growing and dominating the market. But beware that nothing against CI. :)
– Luís Almeida
I intend to migrate p/ Able when I feel confident with the IC, I noticed this tb growth. As for the solution, this way returns Undefined =/
– Lucas Sapienza