Difficulty Loading Input Data via Ajax

Asked

Viewed 92 times

1

I’m trying to load data via Ajax by typing a code and clicking TAB it automatically searches the BD and prints in form inputs. inserir a descrição da imagem aqui

I ran a test on console.log(data) and the data is being returned but does not load us Inputs of the form

inserir a descrição da imagem aqui

Follows the JS Code

// Busco a Chave de Acesso
$(document).ready(function() {
    $("#chave_acesso").change(function() {
        $.ajax({
            method: 'GET',
            headers: Alvo.header(),
            dataType: 'json',
            url:  "{{$urlApiSearchKey}}"+ this.value + "",



            success: function(data) {


                console.log(data);


                $('#obs_nota').val(data.obs_nota);
                $('#pedido').val(data.pedido);

                $('#tipo').val(data.tipo);


            },
            error: function() {
                alert("Código não encontrado!"); },

        });
    });
});
  • Add the relevant part to html, and print the part of the object where obs_note, request and type...

  • the above print is the result of console.log(data)?

1 answer

1


By print posted in question, the returned JSON is coming nested and the block data: is a subitem.

In this case, to get the values, you will have to use data 2x:

$('#obs_nota').val(data.data.obs_nota);
$('#pedido').val(data.data.pedido);
$('#tipo').val(data.data.tipo);

The first data of the return of Ajax and the 2nd of the JSON.

Browser other questions tagged

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