Json Method - Return

1

I am developing a return of zip codes through the Post API, the part that is in the controller is working, but I do not know if the return is correct, nor how to use it 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>
    
asked by anonymous 18.11.2016 / 03:21

2 answers

2

The problem is that you are dealing with the return where you should handle a request error, as below works, you also do not need to parse for Json, the 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);
            }
        });
    });
});

    
18.11.2016 / 12:52
1
  

The part on the controller is working

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

  

but I do not know if the return is correct

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

  

and how to use it in the view.

It's easy. In ajax you have the callback success , it receives as a parameter the return of the pro server request. You only have to use the properties that the resposta object (there on the server) has.

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