Display ajax data, returned by controller in view

Asked

Viewed 554 times

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. :)

  • 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

  • e[key]. nameDoCampo I don’t know CI but Laravel is very show, growing and dominating the market. But beware that nothing against CI. :)

  • 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 =/

1 answer

1


Solved!

If you help someone in the future, what was wrong was that I was not informing the datatype in the ajax request, and because of that I was not able to access the indexes returned in json!

  • Don’t forget to mark your answer later, ok?

  • Hello, I’m new here and I’m still a little lost, so mark the answer?

  • "Accept" your own answer as correct.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.