weixin_33696822 2011-09-11 10:21 采纳率: 0%
浏览 25

ajax重复先前的请求

I'm working with codeigntier and i'm trying some stuff with ajax. This is somewhat difficult to explain.

I have a controller Products with a method "overview", and a view "products_overview".

This is the controller

This is the view

My problem is, is when I make the ajax call in the view in this part:

$('body').delegate('#notification-close', 'click', function(){
    $('#notification').fadeOut(200, function(){
        $('#notification').remove();
    });

    $('#blanket').fadeOut(200).remove();

    $.ajax({
        type: 'GET',
        url: '<?php echo $current_get_url; ?>',
        success : function (result) {
            $('#column-middle').html(result);
        }
    });

});

It doubles the ajax call everytime i use it. And something else. The products controller creates pagination links. When I go back and forth a few times, I also make ajax calls. Let's say I do that 4 times. Then, when I use the above ajax call, it will execute those 4 previous calls and then start doubling from that!

So, i'm kind of lost here. When I put a setTimeout on $('#column-middle').html(result), it will execute once, but then give a jQuery error that "result" is not defined.

  • 写回答

2条回答 默认 最新

  • weixin_33749131 2011-09-11 10:26
    关注

    You should return false from a delegate handler function to stop event bubbling, e.g.:

    $('body').delegate('#notification-close', 'click', function(){
        // processing
    
        // stop further handlers from executing
        return false;
    });
    

    See caveats section here. You can also abuse closure scope properties to prevent call doubling:

    var column_middle_working = false;
    $('body').delegate('#notification-close', 'click', function(){
        if (column_middle_working) return;
        column_middle_working = true;
        // do what you do
    
        $.ajax({
            type: 'GET',
            url: '<?php echo $current_get_url; ?>',
            success : function (result) {
                $('#column-middle').html(result);
                column_middle_working = false;
            }
        });
    
    });
    

    But still recommend you finding a real reason of this behavior.

    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序