显示消息,直到收到ajax响应


Show message until ajax response is received

我想在ajax响应到来之前显示一条错误消息。尝试使用以下代码:

$.ajax({
      url: "user.php",
      type: 'POST',
      data: $('#forgot-form').serialize(),
      beforeSend: function(){
         $('#error').html("Please wait until your request is processed.");
         setTimeout(function() { $("#error").html(''); }, 8000);
       }
     } ).done (function(data) { 
          /* I tried with the commented code also. But this section didn't worked */
          //$('#error').html("Please wait until your request is processed.");
          // setTimeout(function() { $("#error").html('Please wait until your request is processed.'); }, 8000);
          if (data == "success") {  
              $('#user-login').val('');
              $('#error').html("Please check your email to reset your password");
              setTimeout(function() { $("#error").html(''); }, 5000);   
              }  else if (data == "invalid") {
                         $('#error').html("Username or email doesnot exist");
                         setTimeout(function() { $("#error").html(''); }, 5000);
                         $('#user-login').focus();
                         return false;
                 }
         });

user.php页面中,如果用户详细信息正确,我将向用户发送一封电子邮件。所以回应来得很晚。直到那个时候,我想显示等待信息。

此代码正确显示等待消息。但在那之后有一个小的延迟,然后只显示成功的信息。我需要避免这种时间延误。如何更改代码以避免延迟?

有人能帮我做这个吗?提前谢谢。

试试这个:

只需将setTimeout()从发送前回调中删除即可。它将在#error中显示msg,并在ajax完成后替换消息。

 $.ajax({
          url: "user.php",
          type: 'POST',
          data: $('#forgot-form').serialize(),
          beforeSend: function(){
             $('#error').html("Please wait until your request is processed.");
           }
         } ).done (function(data) { 
              /* I tried with the commented code also. But this section didn't worked */
              //$('#error').html("Please wait until your request is processed.");
              // setTimeout(function() { $("#error").html('Please wait until your request is processed.'); }, 8000);
              if (data == "success") {  
                  $('#user-login').val('');
                  $('#error').html("Please check your email to reset your password");
                  setTimeout(function() { $("#error").html(''); }, 5000);   
                  }  
                  else if (data == "invalid") {
                             $('#error').html("Username or email doesnot exist");
                             setTimeout(function() { $("#error").html(''); }, 5000);
                             $('#user-login').focus();
                             return false;
                 }
      });

$.ajax调用之前删除beforeSend()并设置等待消息。

 $('#error').html("Please wait until your request is processed.");
 $.ajax({
    url: "user.php",
    type: 'POST',
    data: $('#forgot-form').serialize(),
 }).done (function(data) { 
    if (data == "success") {  
      $('#user-login').val('');
      $('#error').html("Please check your email to reset your password");
      setTimeout(function() { $("#error").html(''); }, 5000);   
    }  else if (data == "invalid") {
      $('#error').html("Username or email doesnot exist");
      setTimeout(function() { $("#error").html(''); }, 5000);
      $('#user-login').focus();
      return false;
    }
  });