2014-10-29 31 views
0

我想在ajax響應到來之前顯示錯誤消息。試過用下面的代碼:在收到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的頁面,如果用戶信息是正確的,我發送電子郵件給用戶。所以迴應只會遲到。直到那時我想顯示等待消息。

此代碼正確顯示等待消息。但之後有一個小小的延遲,然後只顯示成功消息。我需要避免這個時間延遲。如何更改代碼以避免延遲?

任何人都可以幫助我做到這一點?提前致謝。

回答

1

試試這個:

只是刪除從發送之前調用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; 
       } 
     }); 
+0

@ awlad ..非常感謝。有效。 – Jenz 2014-10-29 09:45:55

+0

@Jenz你好歡迎:) – 2014-10-29 09:48:39

0

刪除您beforeSend()之前$.ajax呼叫設置等待消息。

$('#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; 
    } 
    });