Return on failure to submit

0

I have a submit, which before I perform this function:

$('#modelEditar').submit(function (e) {
var cancelada = $("#Cancelada").val();
if (cancelada == "False") {
    e.preventDefault();
    var id = document.getElementById("nfseid").value;
    var valores = [];
    $('.item').each(function () {
        var entidade = {
            NFSeId: id,
            ProdutoId: parseInt($(this).children()[10].innerText),
            Id: parseInt($(this).children()[11].innerText),
            Codigo: ($(this).children()[1].innerText),
            Descricao: ($(this).children()[2].innerText),
            UnMedida: ($(this).children()[3].innerText),
            Qtd: ($(this).children()[4].innerText),
            ValorUnitário: parseFloat($(this).children()[5].innerText),
            Deducao: parseFloat($(this).children()[6].innerText),
            DescontoCondicionado: parseFloat($(this).children()[7].innerText),
            DescontoIncondicionado: parseFloat($(this).children()[8].innerText),
            ValorTotal: parseFloat($(this).children()[9].innerText),
        };
        valores.push(entidade);
    });
    var obj = {};
    obj.valores = valores;
    var form = this,
        $form = $(form);

    $.ajax({
        url: "/NFSe/SalvaNFSItens",
        type: 'POST',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: JSON.stringify(obj),
        success: function (data) {
            if (data.sucesso == true) {
                if ($('#modelEditar').valid()) {
                    $form.off('submit').submit();
                    if (data.sucesso == false) {
                        console.log('entrou no falso');
                    }
                }
        }
    })
}
else {
    e.preventDefault();
    $("#mensagemalerta").html("NFSe já cancelada, não sendo possível a substituição/alteração.");
    $("#alerta").show();
    $("#alerta").fadeTo(3000, 500).slideUp(500, function () {
        $("#alerta").slideUp(500);
    });
}
})

If everything returns ok, it continues the submit, however in the errors I always return:

return Json(new { resultado = ViewData["Mensagem"], sucesso = false });

And when I try to submit it it looks like this to me in the form:

Iwantedtotreatitthisway:

if(data.sucesso==false){e.preventDefault();$("#mensagemalerta").html(data.resultado);
    $("#alerta").show();
    $("#alerta").fadeTo(3000, 500).slideUp(500, function () {
        $("#alerta").slideUp(500);
    });
}

But I'm not sure how to use it correctly, to report the error dynamically to the user.

    
asked by anonymous 04.01.2019 / 12:32

1 answer

1

If I understand what is happening, the server response is a JSON containing the key

{
  resultado: "Cógido: 0000 ... tente novamente", 
  sucesso: false
}

In your code snippet there is a logical error within the ' success ' key in the AJAX request, since even though the API returns ' success = false ', strong> request failed , so I suggest you do this:

$.ajax({
    url: "/NFSe/SalvaNFSItens",
    type: 'POST',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: JSON.stringify(obj),
    success: function (data) {
        if (data.sucesso == true) {
            if ($('#modelEditar').valid()) {
                $form.off('submit').submit();
                if (data.sucesso == false) {
                    console.log('entrou no falso');
                }
            }
        } else {
            $("#mensagemalerta").html(data.resultado);
            $("#alerta").show();
            $("#alerta").fadeTo(3000, 500).slideUp(500, function () {
                $("#alerta").slideUp(500);
            });
        }
})
    
04.01.2019 / 13:10