无法读取jQuery Ajax错误

无法读取jQuery Ajax错误,jquery,ajax,forms,debugging,Jquery,Ajax,Forms,Debugging,我有一个脚本,应该通过ajax将表单数据发送到PHP脚本。不幸的是,它不起作用,我知道$.ajax有一个错误,但我不知道如何查看该错误,目前我设置了它,所以错误只是警告“失败”。在无法找到错误来源的情况下,解决此问题非常棘手,如何查看此错误?我没有调试ajax的经验:谢谢 $('.jSubmit').click(function() { var form_data = $(this).closest('form').serialize(); va

我有一个脚本,应该通过ajax将表单数据发送到PHP脚本。不幸的是,它不起作用,我知道$.ajax有一个错误,但我不知道如何查看该错误,目前我设置了它,所以错误只是警告“失败”。在无法找到错误来源的情况下,解决此问题非常棘手,如何查看此错误?我没有调试ajax的经验:谢谢

        $('.jSubmit').click(function() {

        var form_data = $(this).closest('form').serialize();
        var form_id   = $(this).closest('form').attr("id");

        function text_success(return_data) {

            if(form_id == "page_form") {
            $('#trans_div').append('<div id="pop_up"><div>Thank you, we will be in touch shortly.</div></div>');
            $('#trans_div').css('display','block').animate({opacity: 0.9}, 800, function() {

                $('#trans_div').delay(1500).animate({opacity: 0.0}, 800, function() {

                        $(this).css('display','none');

                });

            });

            $(this).closest('form').find("input[type=text], textarea").val("");
            }
            else
            {
            //$('.form_bottom_text').html(return_data);
            alert('no');
            }

        }


        function text_failure() {

            if(form_id == "page_form") {
            //$('#trans_div').css('display','block').animate({opacity: 0.9});
            //$('#trans_div').append('<div id="pop_up"><div>Failed to send message.</div></div>');
            //$(this).closest('form').find("input[type=text], textarea").val("");
            alert('failed');
            }
            else
            {
            $('.form_bottom_text').html('Error Occurred');
            }

        }


            $.ajax ({
            url:"form.php",
            type:'POST',
            data: form_data,
            success: function(return_data) {
                text_success(return_data);
                },
            error: function() {
                //text_failure();
                alert('fail');
                }               
            });


        });
$('.jSubmit')。单击(函数(){
var form_data=$(this).closest('form').serialize();
var form_id=$(this).closest('form').attr(“id”);
函数文本\u成功(返回\u数据){
如果(表格id=“页面表格”){
$(“#trans_div”).append('谢谢,我们很快就会联系');
$('#trans_div').css('display','block').animate({opacity:0.9},800,function(){
$('#trans_div')。延迟(1500)。动画({opacity:0.0},800,function(){
$(this.css('display','none');
});
});
$(this).closest('form').find(“输入[type=text],textarea”).val(“”);
}
其他的
{
//$('.form_bottom_text').html(返回数据);
警告(“否”);
}
}
函数文本_失败(){
如果(表格id=“页面表格”){
//$('trans_div').css('display','block').animate({opacity:0.9});
//$(“#trans_div”).append('发送消息失败');
//$(this).closest('form').find(“输入[type=text],textarea”).val(“”);
警报(“失败”);
}
其他的
{
$('.form_bottom_text').html('发生错误');
}
}
$.ajax({
url:“form.php”,
类型:'POST',
数据:表格数据,
成功:函数(返回\u数据){
文本成功(返回数据);
},
错误:函数(){
//text_failure();
警报(“失败”);
}               
});
});

为什么不充分利用正在实现的回调并将适当的参数传递到
“error”

从:

类型:函数(jqXHR jqXHR、字符串textStatus、字符串ERRORSHORN)
请求失败时要调用的函数。函数接收 三个参数:jqXHR(在jquery1.4.x中,XMLHttpRequest)对象,一个 描述发生的错误类型的字符串,以及可选的 异常对象(如果发生)

因此:

error: function() {
    //text_failure();
    alert('fail');
}
应该是这样的:

error: function( x, stat, msg ) {
    //text_failure();
    console.error( x.responseText );
    console.error( x.status );
    console.info( x.statusText );
    console.log( msg );
}

您可能会很快发现发生了什么。

您可以对所有AJAX请求执行类似的操作

$(function() {
    $.ajaxSetup({
        error: function(jqXHR, exception) {
            if (jqXHR.status === 0) {
                alert('Not connect.\n Verify Network.');
            } else if (jqXHR.status == 404) {
                alert('Requested page not found. [404]');
            } else if (jqXHR.status == 500) {
                alert('Internal Server Error [500].');
            } else if (exception === 'parsererror') {
                alert('Requested JSON parse failed.');
            } else if (exception === 'timeout') {
                alert('Time out error.');
            } else if (exception === 'abort') {
                alert('Ajax request aborted.');
            } else {
                alert('Uncaught Error.\n' + jqXHR.responseText);
            }
        }
    });
});
您还可以像这样使用
错误
回调的参数

 $.ajax ({
            url:"form.php",
            type:'POST',
            data: form_data,
            success: function(return_data) {
                text_success(return_data);
                },
error: function(e, status, txt ) {


}
});

检查此项

奇怪的是,整个问题背后的问题是,我的提交按钮的by标签上有一个type=“submit”,修改后修复了此问题。

可能与已尝试的内容重复,警报显示为空/空白我是否检查浏览器控制台?如果是,我仍然没有收到任何消息,这让我很恼火