AJAX request for PHP (CodeIgniter) does not return values

1

I'm working on a situation that I can not resolve for more than 2 days in a row. I have an AJAX that is requesting the values for a controller to populate a select with cities. The request seems to be (no error in debug), but nevertheless nothing comes in return.

JavaScript:

$(function() {
    $('#selectEstado').change(function() {

    var uf = $('#selectEstado').val();

    $.ajax({
        url: "busca/carregaCidades/" + uf,
        dataType: "json",
        success: function(data){
            console.log(data);
        }
    });

    });
});

Controller:

    public function carregaCidades($uf)
    {
        $resultado = $this->busca_model->getCidades($uf);
        return json_encode($resultado);
    }

Model:

    public function getCidades($uf)
    {
        $this->db->where('uf', $uf);
        $dados = $this->db->get('tb_cidades')->result();

        return $dados;
    }

Even with no errors in the console, the result of the request does not come. Thanks for the help

    
asked by anonymous 12.08.2016 / 03:38

1 answer

1

Friend a single detail you sinned was that there in the controller instead of a return json_encode($resultado); you would have to do a echo json_encode($resultado); Ajax will catch what php 'print on screen', and not communicate directly with it, to give you a return it get the value.

Controller:

public function carregaCidades($uf)
{
    $resultado = $this->busca_model->getCidades($uf);
    echo json_encode($resultado);
}

I hope I have helped.

    
21.04.2017 / 19:40