Return not working on Function

Asked

Viewed 68 times

0

I have this function, but the return does not work in it, I do not want it to leave it, until it conforms to the function:

if ($('#FreteComprador').prop("checked") == true) {
        var id = $("#idtransportador").val();
        var frete = $("#Frete").val();
        if (id == "") {
            alert("Preencha o campo transportador corretamente.");
            return;
        } else {
            var url = "/PedidoFornecedor/VerificaCNPJ";
            $.ajax({
                url: url
                , data: { id: id }
                , type: "POST"
                , datatype: "html"
                , success: function (data) {
                    if (data.resultado == true && frete == "0,00") {
                            alert("É obrigatório preencher o valor do frete.");
                            return;
                    }
                }
            });
        }
    }

How can I do it? In other ifs, when I put return works, in this he leaves the function.

  • I believe that there is a problem in your code which is the following, you are firing the ajax before validating the freight. I don’t know about your business rule but this is a little strange, in the Success of the ajax requisition that is testing whether it was filled?

  • It is because he has to check if the freight is by the supplier himself he does not need to be filled in, so this function, if in sucess he returns true, he has to be required to fill in the freight.

  • Where is the problem in the code?

  • He leaves on Return, he should stop there and not enter the next if.

  • try to set Return false;

  • It didn’t work, it goes in the next if, and then back in this.

  • 1

    The function $.ajax by default works in a way async, that is, it will not return the result in the next line of code, for this you must use the callback success:function(){} to continue your checks, remembering that there is also a callback for the case of error in the request. jQuery Ajax

Show 3 more comments

1 answer

1


We’ve been talking and testing, we’re missing put one async: false as a parameter of your ajax

Browser other questions tagged

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