嵌套的jQuery AJAX在错误情况下结束,但没有错误,一切都很好


Nested jQuery AJAX ends up in the error case, but there was no errors and everything worked fine

我有这个jQuery代码:

<script type="text/javascript" >
$(function()
{
    $('#add_comment').bind('submit',function()
    //$("input[type=submit]").click(function()
    {
        var problem_id = $("#problem_id").val();
        var problem_comment = $("#problem_comment").val();  
        var dataString = 'problem_id='+ problem_id + '&problem_comment=' + problem_comment;
        if( problem_id == '' || problem_comment == '' )
        {   
            $('.comment_success').fadeOut(200).hide();
            $('.comment_error').fadeOut(200).show();
        }
        else
        {   
            // Now check if the person is logged in.
            $.ajax({
                    type: "POST",
                    url: "/auth/check_login.php",
                    dataType: "json",
                    success: function(data)
                    {
                        // Now add the comment!
                        $.ajax({
                            type: "POST",
                            url: "/problems/add_problem_comment_ajax.php",
                            dataType: "json",
                            data: dataString ,
                            success: function(data)
                            {
                                $('.add_message_success').fadeIn(200).show();
                                $('.add_message_error').fadeOut(200).hide();
                                // Here can update the right side of the screen with the newly entered information
                                alert ("success");

                            },
                            error: function(data)
                            {
                                alert ("add comment error, data: " + data);
                            }
                        });
                    },
                    error: function(json)
                    {
                        $("#loginpopup").dialog();
                        return false;           
                    }
            });         
        }
        return false;
    });
});
</script>

它实际上工作得很好,并在两个地方调用AJAX,服务器代码的行为是我想要的,我很高兴它是如何工作的。

不幸的是,嵌套的jQuery AJAX调用总是返回到错误情况。我不知道为什么。我怎么才能理解它为什么会这样呢?

我得到没有错误在Chrome的JavaScript控制台或在我的PHP服务器日志。知道是什么吗?

另外,我如何知道返回的数据变量中有什么内容?我现在输出的是"Object Object "

谢谢! !

尝试如下修改错误函数:

$.ajax({
    ...
    error : function (xhr, status, error){
        // Error Text //
        console.log(xhr.responseText); // Server Response
        console.log(xhr.statusText); // Server Status Response
        console.log(error); // Thrown Error
        // Data Object //
        console.log(data); // Displayed in console.
    }
    ...
});

xhr对象是XMLHttpRequest对象的一部分。

所有信息将显示在javascript控制台中。

希望这对你有帮助!