duanla1996 2011-10-14 16:56
浏览 26
已采纳

嵌套的jQuery AJAX最终出现在错误情况中,但没有错误,一切正常

I have this jQuery code:

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

It actually works fine, and calls the AJAX in both places, and the server code behaves as I want it to behave, and I am happy with how it works.

Unfortunately, the nested jQuery AJAX call always goes back into the error case. I don't know why. How can I get to understand why it does that?

I get no errors in the JavaScript console of Chrome or in my PHP server logs. Any idea what it could be?

Also, how can I tell what is in the data variable that is returned? The way I output it now it just says "Object object"

Thanks!!

  • 写回答

1条回答 默认 最新

  • donglei_0507 2011-10-14 17:06
    关注

    Try modifying your error function as follows:

    $.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.
        }
        ...
    });
    

    The xhr object is part of the XMLHttpRequest object.

    All info will appear in the javascript console.

    Hope this helps!

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

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)