Extract data in JQuery

0

I'm having a problem with Ajax ... I can not extract the data. They are returned, however, at the time of printing, nothing appears.
The following codes: Model:

function livros() {
        $this->db->select('idobras,titulo,idbib_exemplares,bib_obras_idobras');
        $this->db->from('bib_exemplares');
        $this->db->join('bib_obras', 'idobras = bib_obras_idobras');
        $this->db->where('estado','Ativo');
        $this->db->order_by('titulo','asc');
        $result = $this->db->get();

        if ($result->num_rows() > 0) {
            return  $result->result();
        } else {
            return false;
        }
    }

Controller:

function GetLivros() {
        $book = $this->bib_movimentacao_model->livros();
        if ($book) {
            echo json_encode($book);
        }else{
            echo json_encode('');
        }
    }

JQuery snippet:

print = '<div id="rem">'+
    '<blockquote>'+
        '<div class="form-inline">'+
            '<label>Livro:&nbsp;</label>';

            $.ajax({
                'url': 'bib_movimentacao_controller/GetLivros',
                'type': 'POST',
                'data': {

                 },
                'success': function(data){
                    var result = JSON.parse(data);
                    alert(data);
                    print += '<select name="livros[]" id="livros" class="form-control" data-live-search="true" style="width: 80%">';
                        $.each(result, function(index, val){
                            print += '<option value="'+val.idbib_exemplares+'">'+val.titulo+'</option>';
                        });
                    print += '</select>';
                }
            });

print += '</div>'+

On this alert, I saw that the data is being fetched correctly.   Thanks in advance for your help, guys. PS: Here's how it goes:

    
asked by anonymous 14.04.2016 / 16:12

1 answer

0

The ajax operation is done asynchronously, that is, it is only concatenating that text in the variable when the request is returned. Move the print variable and the rest of the code into success and the problem will be solved.

    
14.04.2016 / 16:25