PHP json for Ajax request

0

I have the following function in Class Read.php :

public function selectCliente(){

        try{

            $stmt = $this->conn->prepare("SELECT 
                                                 CLIENTE.CODIGO_CLIENTE, 
                                                 CLIENTE.CGC, 
                                                 CLIENTE.RAZAO, 
                                                 CLIENTE.FANTASIA, 
                                                 CLIENTE.TELEFONE1 
                                          FROM 
                                                 CLIENTE");
            $resultCliente = $stmt->execute();

            $record = array();

            foreach($resultCliente as $group)
            {
                $record[$group['CODIGO_CLIENTE']]['CGC']         = $group['CGC'];
                $record[$group['CODIGO_CLIENTE']]['RAZAO']       = $group['RAZAO'];
                $record[$group['CODIGO_CLIENTE']]['FANTASIA']    = $group['FANTASIA'];
                $record[$group['CODIGO_CLIENTE']]['TELEFONE1']   = $group['TELEFONE1'];         
            }

            $output = array();

            foreach( $record as $id => $dados)
            {
                $output[] = array(
                        'CODIGO_CLIENTE' => $id,
                        'CGC'            => $dados['CGC'],
                        'RAZAO'          => $dados['RAZAO'],
                        'FANTASIA'       => $dados['FANTASIA'],
                        'TELEFONE1'      => $dados['TELEFONE1']
                );      
            }


            $jsonCliente = json_encode($output, JSON_UNESCAPED_UNICODE);            
            return $jsonCliente;

        }catch (PDOException $exception){
            header("Location: ./error.php?err=Unable-to-select");
            echo 'Erro: '.$exception->getMessage();
            return null;
        }//Try end

    }//Function Select End

This code gives the following return in client.php :

[{"CODIGO_CLIENTE":3,"CGC":"78.079.128\/0001-80","RAZAO":"Cliente 2","FANTASIA":"Cliente 2","TELEFONE1":"+99(99)9999-9999"},
{"CODIGO_CLIENTE":2,"CGC":"14.617.787\/0001-40","RAZAO":"Cliente 1","FANTASIA":"Cliente 1","TELEFONE1":"+99(99)9999-9999"},
{"CODIGO_CLIENTE":5,"CGC":"54.731.556\/0001-87","RAZAO":"Cliente 3","FANTASIA":"Cliente 3","TELEFONE1":"+99(99)9999-9999"},
{"CODIGO_CLIENTE":6,"CGC":"55.765.452\/0001-56","RAZAO":"Cliente 4","FANTASIA":"Cliente 4","TELEFONE1":"+99(99)9999-9999"}]

Ajax code:

    $('#btn-sinc').click(function() {   
    $.ajax({
        type : "POST",
        url : "http://localhost/read/cliente.php",
        crossDomain: true,      
        contentType: "application/json; charset=utf-8",
        dataType: 'json',       
        success : function(responseData, textStatus, jqXHR) {

                var html = "";

            for(var i=0; i < responseData.length; i++){

                    html += '<tr>'; 
                    html += '<td data-label="Cnpj">'+responseData[i].CGC+'</td>';
                    html += '<td data-label="Razão">'+responseData[i].RAZAO+'</td>';                    
                    html += '<td data-label="Fantasia">'+responseData[i].FANTASIA+'</td>';                  
                    html += '<td data-label="Telefone">'+responseData[i].TELEFONE1+'</td>';                          
                    html += '</tr>';                   
            }

            $('.table').html(html);         
        },
        error: function (responseData, textStatus, errorThrown) 
        {
            console.warn(responseData, textStatus, errorThrown);
            alert('Falha');
        }
    }); 
});

I have managed to resolve it, I have edited it to let it respond.

    
asked by anonymous 25.10.2016 / 19:13

0 answers