Extract data in Jquery

Asked

Viewed 101 times

0

I am having a problem with Ajax... I am not able to extract the data. They are returned, however, at the time of printing, nothing appears.
Follows 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('');
        }
    }

Excerpt from the Jquery:

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 that Alert, I saw that the data is being brought in correctly.
Thanks for the help, guys.
P.S.: Follows image of how it looks:
inserir a descrição da imagem aqui

1 answer

0


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

  • Thank you so much for your help, Gabriel! It worked!! Ooo/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.