Problem with function return

1

Well, I have a json code that looks for logged-in user information, and later, I have another code that looks for all the sectors registered in the system. The function normally returns the information, but when return the desired value in the function always goes as undefined . Here is the code:

$(document).ready(function () {
        $("input[type=radio]").bind("click", function () {
            if ($("input[type=radio]:checked").val() == "Privado") {
                $.ajax({
                    url: "@Url.Action("GetSetores", "Json")",
                    dataType: "json",
                    type: "POST",
                    error: function () {
                        alert("Ocorreu um erro ao carregar os setores!");
                    },
                    success: function (data) {
                        var items = "";
                        var idSetorUsuarioLogado = setorUsuarioLogado();
                        $.each(data, function (i, item) {
                            if (item.Id == idSetorUsuarioLogado) {
                                items += "<div><input type='checkbox' class='setores' name='setors' value='" + item.Id + "' id='SetoresAcesso' checked>" + item.Nome + "</div>";
                            } else {
                                items += "<div><input type='checkbox' class='setores' name='setors' value='" + item.Id + "' id='SetoresAcesso'>" + item.Nome + "</div>";
                            }
                        });
                        $('#ListaDeSetores').html(items);
                    }
                });
            } else {
                $('#Setor').html('');
            }
        });
    });

    function setorUsuarioLogado() {
        var idSetorUsuarioLogado;
        $.ajax({
            url: "@Url.Action("CarregaDadosUsuario", "Json")",
            type: "GET",
            error: function () {
                alert("Ocorreu um erro ao carregar o setor do usuário logado");
            },
            success: function (resultado) {
                idSetorUsuarioLogado = parseInt(resultado.IdSetor);
            }
        });
        return idSetorUsuarioLogado;
    }
    
asked by anonymous 26.07.2015 / 20:42

1 answer

0
success: function (data) {
    var items = "";
    var idSetorUsuarioLogado = setorUsuarioLogado();

    var json = JSON.parse(data);

    $.each(json, function() {

      $.each(this, function(i, item) {

        if (item.Id == idSetorUsuarioLogado) {
            items += "<div><input type='checkbox' class='setores' name='setors' value='" + item.Id + "' id='SetoresAcesso' checked>" + item.Nome + "</div>";
        } else {
            items += "<div><input type='checkbox' class='setores' name='setors' value='" + item.Id + "' id='SetoresAcesso'>" + item.Nome + "</div>";
        }

      });

    });

}

Try this way the "sucess".

    
27.07.2015 / 01:56