Return object json to ajax in C #

0

Function in ajax

function RefreshFuncaoOrdenamentoJornadaSalario() {
        idOrdenamento = $("#DDOrdenamento").val();
        idCategoria = $("#DDCategoria").val();
        GrauNumero = $("#DDGrau").val();
        $.ajax({
            type: "GET",
            url: '@Url.Action("VerificaJornadaSalario", "CargoSalarios")',
            data: { idOrdenamento: idOrdenamento, idCategoria: idCategoria, GrauNumero: GrauNumero },
            async: false,
            sucess: function (result) {
                var obj = jQuery.parseJSON(result);
                alert(obj);
            }
        });
    }

Controller that populates the object

 [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult VerificaJornadaSalario(short? idOrdenamento, short? idCategoria, short? GrauNumero)
        {
            int? jornada= contextREmpr.Salario
               .Count(x => x.idOrdenamentoLegal == idOrdenamento
                           && x.idCategoriaDeFuncao == idCategoria);
            string msgjornada = null;
            if (jornada>0)
            {
                int? verificajornada = contextREmpr.Salario
                .FirstOrDefault(x => x.idOrdenamentoLegal == idOrdenamento
                            && x.idCategoriaDeFuncao == idCategoria).JornadaCompleta;
                if (verificajornada == 1)
                { 
                    msgjornada = "Jornada de Trabalho Completa";
                }
                else
                {
                    msgjornada = "Jornada de Trabalho Parcial";
                }
            }
            else
            {
                ViewBag.Jornada = "Não existe jornada de trabalho para essa configuração de ordenamento";
            }
            var result = new { jornada = msgjornada, salario = "0" };
            return Json(result, JsonRequestBehavior.DenyGet);
        }

I need to put the values of the object in a textbox element, I used the alert to see if the object is arriving, where did I go wrong?

    
asked by anonymous 22.06.2016 / 22:14

1 answer

0

Regarding the alert, I do not know. However I recommend putting a ' dataType: "json"', as this automatically converts the result to an object in javascript. This way you will not need to put the "jQuery.parseJSON (result)". You can deal with it directly. Your code will simply be "var obj = result".

And are you sure the result is getting the content? Put a "console.log (result)" and see in the devtool of Google Chrome if the console prints the result you want to receive from the backend.

    
23.06.2016 / 05:22