Problem with function return

Asked

Viewed 66 times

1

Guys, I have a json code that searches for logged user information, and later, I have another code that searches all sectors registered in the system. The function normally returns the information, but when it returns the desired value in the function always goes as undefined. Follow the code below:

$(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;
    }
  • if Voce makes a console.log in the result.Idsector within Success the value will appear ? if yes Return is probably running before Success occurs, if so try to give the Return in or create a global variable and assign this result to it that will solve the problem...

  • I recommend a read on this post, it seems to me and the same problem as yours: http://stackoverflow.com/questions/5316697/jquery-return-data-after-ajax-call-success

  • could not return the result of "setorUsuarioLog" along with the data of the first requsition? So it would avoid a new requisition and eliminate the problem.

  • At which specific point (line) of the code the error is occurring?

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".

Browser other questions tagged

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