Problem with form + ajax

Asked

Viewed 56 times

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);
  }

1 answer

0


In jQuery’s Ajax, the success: already means that the code status of the return XHR was 200. It is redundant to make a OK (that would be the xhr.statusText in the third return parameter) inside the callback, since it will always be "OK" in the success::

success: function (result, status, xhr){

   // result -> dados HTML de retorno
   // status -> retorna a string "success"
   // xhr    -> objeto XHR gerado

}

To get the return codes 200 and OK:

success: function (result, status, xhr){

   xhr.status; // retorna "200"
   xhr.statusText; // retorna "OK"

}
  • 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 ?

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

  • 1

    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.

  • ah yes, I get it.

  • 1

    I will update the answer. I can get the "OK", but it would be redundant because the Success already means that everything OK.

  • It is that I need to treat an error, because when there occurs a success it appears the message saying it was successful.

  • 1

    Take a look now.

Show 2 more comments

Browser other questions tagged

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