Metodo Json - Return

Asked

Viewed 996 times

1

I am developing a return of Zip Code through the post office API, the part that is in the controller is working, but I do not know if the return is correct, nor how to use in the view. If anyone can give me a Light I’m Grateful.

Controller:

    public JsonResult RetornaEndereco(string cep)
    {
        var valor = cep;
        var ws = new WSCorreios.AtendeClienteClient();
        var resposta = ws.consultaCEP(valor);
        try
        {

            System.Console.WriteLine();
            System.Console.WriteLine("Endereço: {0}", resposta.end);
            System.Console.WriteLine("Complemento: {0}", resposta.complemento);
            System.Console.WriteLine("Complemento 2: {0}", resposta.complemento2);
            System.Console.WriteLine("Bairro: {0}", resposta.bairro);
            System.Console.WriteLine("Cidade: {0}", resposta.cidade);
            System.Console.WriteLine("Estado: {0}", resposta.uf);
            System.Console.WriteLine("Unidades de Postagem: {0}", resposta.unidadesPostagem);
        }
        catch (Exception ex)
        {
            return Json("Erro ao efetuar busca do CEP: {0}", ex.Message);
        }
        return Json(resposta);
    }

The Script:

<script>
    $(document).ready(function () {
        $("#cep").blur(function () {
            var cepValue = $(cep).val();
            $.ajax({
                type: 'POST',
                url: 'RetornaEndereco',
                data: { cep: cepValue },
                dataType: 'json',
                success: function (data) {
                    var text = data;
                },
                error: function (data) {
                    alert('Error' + data);
                    obj = JSON.parse(text);
                    document.getElementById("demo").innerHTML =
                    obj.data.cep;
                }
            });
        });
    });
</script>

2 answers

2


The problem there is that you are dealing with the return where you should deal with the request error, the way below works, also you do not need to parse for Json, jquery already returns ready.

<script>
$(document).ready(function () {
    $("#cep").blur(function () {
        var cepValue = $(cep).val();
        $.ajax({
            type: 'POST',
            url: 'RetornaEndereco',
            data: { cep: cepValue },
            dataType: 'json',
            success: function (data) {                                        
                document.getElementById("demo").innerHTML = data.cep;
            },
            error: function (data) {
                alert('Error' + data);
            }
        });
    });
});

  • Thanks @Fernandoromano I didn’t pay attention to this.

  • It’s nois! Most problems are small details.

1

The part on the controller is working

Okay, but those System.Console.WriteLine are completely unnecessary. Another thing, return ex.Message when an error occurs can be a problem. That way you return only the main message, all other information about the error goes away.

but I don’t know if the return is correct

If the method in controller is correct, the return is correct. Provided that resposta be an object, it will be returned.

and how to use in the view.

It’s easy. In ajax you have the callback success, it takes as parameter the return of the request to the server. Then you just use the properties that the object resposta (server there) has.

success: function (data) {
    document.getElementById("txt-cep").innerHTML = data.cep;
}

Browser other questions tagged

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