2014-04-14 97 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',

相关问题