How to get the result value of divValue. result[2] in the mvc controller?

Asked

Viewed 43 times

0

$(document).ready(function(){
        $("#btnSend").click(function(){
            $.ajax({
                url:'@Url.Action("VerificaCep","ConsultaCep")',
                type: "GET",
                contentType:"application/json",
                data: { cep: $("#txtCep").val() },
                success: function (result) {
                    $("#divValor").html("<label>Endereço:" + result[0] + "   Bairro:" + result[1] + "  Cidade:" + result[2] + "  Estado:" + result[3] + " </label>")
                },

                error: function(xhr, exception){
                    alert("Favor digitar o CEP!")
                }
            });

        });
        var Valor = document.getElementById('divValor.result[2]').innerHTML;
    });
  • The script works correctly, I just can’t get the result of Array[2] for the Controller because I will do a search by client city. Excuse the question but I tried everything and nothing. Since thank you very much

  • Wouldn’t it just be, add in the date? Getting data: { cep: $("#txtCep").val(),divvalor: $('#divValor').text() }, or something like that?

1 answer

0

2 errors: First there is no div with id = divValor.result[2], This label is used for all values. Second mistake result[n] needs to be inside the function success: function (result)

var Valor;
$(document).ready(function(){
        $("#btnSend").click(function(){
            $.ajax({
                url:'@Url.Action("VerificaCep","ConsultaCep")',
                type: "GET",
                contentType:"application/json",
                data: { cep: $("#txtCep").val() },
                success: function (result) {
                    $("#divValor").html("<label>Endereço:" + result[0] + "   Bairro:" + result[1] + "  Cidade:" + result[2] + "  Estado:" + result[3] + " </label>");
                    valor = result[2];
                    console.log(valor);
                },

                error: function(xhr, exception){
                    alert("Favor digitar o CEP!")
                }
            });

        });

    });
  • Perfect Hudson, value gets the correct index, how can I now call the value in Controller? Thank you so much

  • It is already calling the get method, now if you want to send something to your controller you can use the post method

Browser other questions tagged

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