Popular second select after selecting the first select [closed]

1

Good evening!

I want to populate the second select with the data depending on the first select, I have the following code

View

$(document).ready(function(){
   $("#relatorio").change(function(){
      $.ajax({
         type: "POST",
         url: "carregaDados",
         data: {relatorio: $("#relatorio").val()},
         dataType: "json",
         success: function(json){
            console.log(json);
            var options = "";
            $.each(json, function(key, value){
               options += '<option value="' + key + '">' + value + '</option>';
            });
            $("#filtro").html(options);
         }
      });
   });
});
<div class="form-group">
                                            <label class="col-md-3 control-label">Relatório</label>
                                            <div class="col-md-9">                                                                                            
                                                <select class="form-control" name="relatorio" id="relatorio">
                                                    <option value="0">Escolha um relatório</option>
                                                    <option value="chamadosCliente">Chamados por cliente</option>
                                                    <option value="chamadosTecnico">Chamados por Tecnico</option>
                                                </select>
                                            </div>
                                        </div>

                                       <div class="form-group">
                                            <label class="col-md-3 control-label" for="filtro">Filtro</label>
                                            <div class="col-md-9">                                                                                            
                                                <select class="form-control" name="filtro" id="filtro">

                                                </select>
                                            </div>
                                        </div>

Controller

public function carregaDados(){

        $this->load->model('clientes_model');
        $clientes = $this->clientes_model->buscaNomeCliente();
        echo json_encode($clientes);
}

Model

public function buscaNomeCliente(){
        $this->db->select('id, razaoSocial');
        $query = $this->db->get('clientes');
        return $query->result();
    }

But when it returns the pro ajax data in the View, it returns:

Array [ Object, Object ]

Do not return the data as it has to be, can anyone help me?

Thank you!

    
asked by anonymous 24.09.2015 / 03:57

1 answer

0

I noticed who in your controller you return the data through the line json_encode($clientes);

This causes you to return an object with its JSON representation. So, in the successful response of your AJAX, you need to convert it back so you can interpret your object.

For this to occur, you must include the following line within your success AJAX callback:

json = JSON.stringify(json);

Now your object is converted to the JSON String type.

Documentation:

link

link

    
24.09.2015 / 15:15