1
How do I use the JSON return in HTML? Follow the codes.
Ajax function:
$(document).ready(function(){
$.ajax({
url:"<?=base_url('pedido/listar');?>",
dataType: "html",
success: function(data)
{
$('#pedidos').html(data);
}
});
});
Controler:
public function listar()
{
$this->pedido_model->listarPedidos();
}
Model:
public function listarPedidos()
{
$this->db->select('*');
$query = $this->db->get('pedidos');
foreach ($query->result_array() as $pedido){
$result[] = array('dados' => '<div class="card comandas text-center" href="#exampleModalCenter" data-toggle="modal">
<div class="card-header">
'.$pedido['cliente'].'
</div>
<div class="card-body">
<h5 class="card-title">'.$pedido['idPedido'].'</h5>
<p class="card-text badge badge-success">Aberto</p>
</div>
</div>');
}
echo json_encode($result);
}
The best thing to do is to return the object with the information and use this information in javascript to mount your html.... I will set an example and put as an answer.
– Lucas Gauna