2014-04-14 98 views
2

我有一個使用ajax發送郵件的腳本。我已通過檢查將收到郵件的電子郵件進行測試,結果足夠正確,ajax請求成功。我還檢查了我的Firefox瀏覽器的控制檯窗口,它也向我顯示了一個成功的消息。但我的問題在於,代替done回調函數,會觸發error回調函數。您可能都想知道爲什麼我仍在使用error功能而不是fail。原因是因爲當我嘗試使用fail函數時,它不會觸發我在其中設置的alertbox。所以我做的是回去並再次使用error函數,因爲至少它觸發了我所做的alertbox。即使請求成功,也會觸發錯誤回調函數

下面是腳本:

<script type="text/javascript"> 
    var submitButton = $('#submit');    // Variable to cache button element 
    var alertBox1 = $('.success');     // Variable to cache meter element 
    var alertBox2 = $('.alert'); 
    var closeButton1 = $('.close1');     // Variable to cache close button element 
    var closeButton2 = $('.close2');     // Variable to cache close button element 

    $(function(){ 
     $('#contactform').submit(function(e){ 
      e.preventDefault(); 
      console.log('hello'); 
      var formData = $(this).serialize(); 
      console.log(formData); 

      $.ajax({ 
       type: 'POST', 
       url: 'send.php', 
       data: formData, 
       dataType: 'json', 
       done: function(){ 

         $(submitButton).fadeOut(500); // Fades out submit button when it's clicked 
         setTimeout(function() { // Delays the next effect 
          $(alertBox1).fadeIn(500); // Fades in success alert 
         }, 500); 
       }, 
       error: function(){ 
        $(submitButton).fadeOut(500); // Fades out submit button when it's clicked 
         setTimeout(function() { // Delays the next effect 
          $(alertBox2).fadeIn(500); // Fades in fail alert 
         }, 500); 
       } 
      }); 
     }); 

     $(closeButton1).click(function() { // Initiates the reset function 
      $(alertBox1).fadeOut(500); // Fades out success message 
      setTimeout(function() { // Delays the next effect 
       $('input, textarea').not('input[type=submit]').val(''); // Resets the input fields 
       $(submitButton).fadeIn(500); // Fades back in the submit button 
      }, 500); 

       return false; // This stops the success alert from being removed as we just want to hide it 
     }); 

     $(closeButton2).click(function() { // Initiates the reset function 
      $(alertBox2).fadeOut(500); // Fades out success message 
      setTimeout(function() { // Delays the next effect 
       $('input, textarea').not('input[type=submit]').val(''); // Resets the input fields 
       $(submitButton).fadeIn(500); // Fades back in the submit button 
      }, 500); 

       return false; // This stops the fail alert from being removed as we just want to hide it 
     }); 
    }); 
</script> 

什麼似乎是一個造成的?只是爲了重申,我試過使用fail而不是error回調函數,因爲這是我在互聯網上找到的答案之一,也因爲我知道error函數已被棄用。但由於我上面提到的原因,我別無選擇,只能使用它。

+1

你是否從服務器返回任何json? –

+2

嘗試使用成功,而不是完成 – Ritikesh

+0

我從來沒有看到「成功」的術語被應用於成功的回調,正如@Ritikesh說的,而是嘗試使用「成功」附加回調 –

回答

2

如果你參考了文檔,你不能在ajax函數裏面用做回調函數。在ajax調用結束時使用成功或添加完成。

$.ajax({ 
    // url, data etc 
success: function() { 
    //success handler 
}, 
error:function(){ 
    //Error handler 
} 
}); 

    (OR) 
$.ajax({ 
     // ajax related codes 
}).done(function(){ 
    //callback 
}); 

此外,如果你是不是真的從服務器返回的JSON,請從Ajax調用的dataType: 'json',

相關問題