jQuery ajax请求没有完成

时间:2011-11-01 11:26:59

标签: apache jquery

我面临一个奇怪的问题,即当后续请求发出时,ajax请求无法完成。

第一次加载网页时,ajax调用正常。但是如果我在不刷新页面的情况下调用相同的方法,那么请求永远不会完成,甚至apache也没有响应。我检查了apache错误日志,找到了以下条目:

[Tue Nov 01 16:41:42 2011] [error] server reached MaxClients setting, consider raising the MaxClients setting

以下是我要执行的jQuery代码:

(function($) {
    $(function(){

        form = $('form.crud');
         $("#end_date").datepicker({dateFormat: 'yy-mm-dd'});

/** Contract Comments **/
        $('#actionbar ul li:nth-child(1) a').colorbox({
            scrollable: false,
            innerWidth: 600,
            innerHeight: 280,
            href: SITE_URL + 'admin/contracts/comments/create_ajax',
            onComplete: function() {
                $.colorbox.resize();
                $('form#comments').removeAttr('action');
                $('form#comments').live('submit', function(e) {
                    var form_data = $(this).serialize();

                    $.ajax({
                        url: SITE_URL + 'admin/contracts/comments/create_ajax',
                        type: "POST",
                            data: form_data,
                        success: function(obj) {
                            if(obj.status == 'ok') {
                                $.colorbox.close();
                            } else {
                                $('#cboxLoadedContent').html(obj.message + obj.form);
                                $('#cboxLoadedContent p:first').addClass('notification error').show();
                            }
                        }
                    });
                    e.preventDefault();
                });
            }
        });
    });
})(jQuery);

浏览器似乎充斥着冗余请求。增加MaxClients计数是否有意义(目前对于prefork MPM为256,对于worker MPM为300。

1 个答案:

答案 0 :(得分:1)

似乎你有效地淹没了你的网络服务器。您应该使用Firebug或Chrome开发人员工具等工具,并查看您要发送的ajax请求的数量。

我请求$('form#comments').live('submit', function(e) {中的直播活动多次运行(请使用console.log('attaching live event');进行检查)。尽量避免直播事件或至少在绑定的事件元素中添加一个类,以便您可以过滤实时事件选择器以避免绑定已绑定的事件。

这应该有效:

$("form#comments:not(.live-submit-binded)")
   .addClass('live-submit-binded')
   .live('submit', function(e) {
    (...)
});
相关问题