Load var into jquery with data from BD by Controller

Asked

Viewed 142 times

0

To var str has script with fixed values. How do I get these values to be loaded from a BD through a controller? I already have the method that brings me this.

   var str = "";
    var data = [];

    $(document).ready(function () {
        $("#btnPesquisarCnpj").click(function () {

            var valor = $("#txtCnpjPesquisa").val();

            if (isCpfCnpj(valor)) {
                return true;

            }
            else {
                $("#txtCnpjPesquisa").focus();

                str += '';
                str += '<label>CNPJ digitado incorretamente!</label>';
                str += '<div>';
                str += '<b><label>Script para Central de Atendimento:</label></b></div>';
                str += '<div><label>Digite novamente o CNPJ, se possível peça que repitam pausadamente.</label>';
                str += '</div>';
                str += '';
                str += '';
                str += '<div>';
                str += '<b>Script para Suporte Técnico:</b></div>';
                str += '<div><label>Digite novamente o CNPJ.</label></div>';

                $('#filtroPesquisa').html(str);

                return false;
            }
        });
    });

1 answer

1

In your code there is no ajax call to the server to recover the data you want. Basically, you need to make a call via ajax to the server in the URL responsible for recovering this data. This can be done with jQuery like this

var opcoes = {
    type: 'GET',
    dataType: 'json',
    url: 'servidor/caminho-dados'
}

$.ajax(opcoes).then(function (data) {
    // instruções para operar sobre os dados retornados
}).fail(function (jqXHR, textStatus, errorThrown) {
    //instruções para lidar com o erro apresentado
});

You can use this in your code at the time of recovering the data and operate on it. You can read more about using ajax with jQuery in the jQuery documentation itself here.

Browser other questions tagged

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