douqiang1851 2014-04-14 09:37
浏览 93
已采纳

即使请求成功,也会触发错误回调函数

I have a script that uses ajax to send mail. I have tested it by checking the email that will receive the mail and true enough, the ajax request is successful. I also checked the console window of my Firefox browser and it also shows me a successful message. But my problem here is that instead of the done callback function, the error callback function is fired. You may all wonder why I'm still using the error function instead of fail. The reason for this is because when I tried using the fail function, it doesn't trigger the alertbox that I have set inside it. So what I did is go back and use error function again since at least it triggers the alertbox I made.

Here is the script:

<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>

What seems to be the one causing this? Just to reiterate, I've tried using fail instead of error callback function since that is one of the answers I found in the Internet and also because I know for a fact that the error function is already deprecated. But because of the reason I mentioned above, I've no choice but to use it.

  • 写回答

1条回答 默认 最新

  • dqnqpqv3841 2014-04-14 09:45
    关注

    if you refer the documentation, you cannot use done inside the ajax function as a callback. Either use success or add done at the end of ajax call.

    $.ajax({
       // url, data etc
    success: function() {
        //success handler
    },
    error:function(){
       //Error handler
    }
    });
    
         (OR)
    $.ajax({
          // ajax related codes
    }).done(function(){
         //callback
    });
    

    Also if you aren't really returning JSON from the server, remove the dataType: 'json', from the ajax call.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办