How to Parse/get Ajax delayed response

The Naga Tanker

I'm trying to get the response code for an email form I sent to a PHP mailer page with ajax. If the mail is sent successfully it echos 1 else 0.

Here is how I send the from

$('form.ajax-form').on('submit', function(evt){
  evt.preventDefault();
  var $form = $(this);
  $.ajax({
    url: $form.attr('action'),
    method: $form.attr('method'),
    data:$form.serialize(),
    success: function(response){
      if (response == '1' ) {
        M.toast({html: 'I am a toast!'});
      }else if (response == '0') {
        M.toast({html: 'I am not a toast!'});
      }
    },
  });
});

Depending on the response I want to execute M.toast({html: 'I am a toast!'}); but i cant get the response even if the PHP mailer echos 1 or 0. It takes 4 seconds for the mailer to echo the response. I have tried delay : 4 Still can't get the response. What am i doing wrong ? Please Help. Thank you.

UPDATE.

Here are the response from XHR.

enter image description here

enter image description here

enter image description here

Zeynal

Try this code:

$('form.ajax-form').on('submit', function(evt){
  evt.preventDefault();
  var $form = $(this);
  $.ajax({
    url: $form.attr('action'),
    method: $form.attr('method'),
    data:$form.serialize(),
    success: function(response){
      if ($.trim(response) === '0') {
        M.toast({html: 'I am not a toast!'});
      }else{
        M.toast({html: 'I am a toast!'});
    },
  });
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related