weixin_33690963 2016-03-07 14:21 采纳率: 0%
浏览 8

Ajax表单提交错误

I am using the code below to send an email via ajax. The problem is that even if the email is not sent e.g i localhost or when there is no internet connection, the success command will still be returned by the script. Kindly advise me on what modifications I should make to the code for it to detect whenever an error is encountered.

$('#send_button').click(function(e) {
     $("#sending").removeClass("hidden_div");//just to know that we have some data

     alerts = '';
     if($("input[name=names]").val() == ''){
        alerts += "1";
        $("#error_name").removeClass("hidden_div");
     }

     if(alerts != ''){
        $("#sending").addClass("hidden_div");
     } else {
         $.ajax({
                url:'index.php',
                type:'POST',
                data:$("#form").serialize(),
                cache: false,
                success: function(data){ 
                    show_ok();
                },
                error: function(){
                    $("#msg_not_sent").removeClass("hidden_div");
                    $( "#msg_not_sent" ).empty().append( "An error has occurred. Kindly try again." );
                  }
        });     }
    e.preventDefault();
});
  • 写回答

2条回答 默认 最新

  • ?yb? 2016-03-07 14:27
    关注

    I believe this happens if the code you are calling doesn't error in the way jQuery expects, or you aren't using a matching return data type you can get this. There's a way around it though.

    complete: function (data) {
        if (data.status == "200") {
        }
    
    评论

报告相同问题?