1
I made an ajax request to send the data from a form to the server, the data is sent and stored perfectly. But when processing the Success creating the condition, should the return of result were the standard 200 or "OK" would send a message of success and if not, the error message. However, in doing so I realized that the result was not returning because it already fell straight into Isis. And even dropping ELSE, it stores the data on the server, but throws the error message.
function sendForm()
{
$("#divSucesso").hide();
$("#divError").hide();
document.getElementById("btnEnviarForm").disabled = true;
// var formAgenda = document.getElementById("form");
var formData = new FormData (document.forms[0]);
$.ajax({
url: 'insert.php',
type: 'POST',
async: true,
dataType: 'html',
data: formData,
processData: false,
contentType: false,
success: function (result) {
if (result.substring(0, 2) == "OK")
{
document.getElementById('divSucesso').innerHTML = "Dados salvos com sucesso: " + result;
$("#divSucesso").stop().fadeIn(200).delay(50000).fadeOut(200);
document.getElementById("form").reset();
}
else
showMessageError(result);
document.getElementById("btnEnviarForm").disabled = false;
},
error: function (xhr, status, error) {
showMessageError(status + error + xhr.responseText);
document.getElementById("btnEnviarForm").disabled = false;
}
});
}
function showMessageError(message)
{
document.getElementById("errorMsg").innerHTML = message;
$("#divError").fadeIn(200).delay(2500).fadeOut(200);
}
It didn’t work. What I did was put the result on an Alert and see the return, the same is the data collected from the form. knows tell me why ?
– Gabriel
standard response that would be when td OK occurs when taking data to the server. OK or 200 if it was right, and 404 would be error. But the handle is there, it stores in the database but the result is not OK or even 200, it is taking the data that is inserted in the form.
– Gabriel
It’s not like that, you’re confusing things. In jQuery’s Ajax, Success: it means that everything worked out and the return is the HTML of the requested page. Using XHR in pure JS is what you check the status of the request.
– Sam
ah yes, I get it.
– Gabriel
I will update the answer. I can get the "OK", but it would be redundant because the Success already means that everything OK.
– Sam
It is that I need to treat an error, because when there occurs a success it appears the message saying it was successful.
– Gabriel
Take a look now.
– Sam