0
I have a Ubmit, 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 it returns everything ok, it continues the Ubmit, but in the errors I always return:
return Json(new { resultado = ViewData["Mensagem"], sucesso = false });
And when I try Submit it appears to me like this on the form:
I wanted to treat this way:
if (data.sucesso == false)
{
e.preventDefault();
$("#mensagemalerta").html(data.resultado);
$("#alerta").show();
$("#alerta").fadeTo(3000, 500).slideUp(500, function () {
$("#alerta").slideUp(500);
});
}
However I am not sure how to use correctly, to inform the error dynamically to the user.
Tried to decode JSON with
JSON.parse(data)
before accessing the values?– edson alves
No, how can I?
– Mariana
Add that code snippet
data = JSON.parse(data)
beforeif (data.sucesso == true)
– edson alves
I tried to put here
if ($('#modelEditar').valid()) {
 data = JSON.parse(data);
 console.log(data);
 //$form.off('submit').submit();

 }
However it is returning the error Unexpected token o in JSON at position 1– Mariana
It has to be in Success
– edson alves
It’s because I have a comeback, and then I have another
($('#modelEditar').valid())
I have another return, if I put before, it will always be true, because it is catching the first, I need the next return– Mariana
If it doesn’t work, remove this code I told you about and change your
dataType: "json"
capital letters, like this :dataType: "JSON",
– edson alves
I think I got your question wrong. You’re already getting the value correctly with parsed JSON... What’s the problem actually?
– edson alves