【问题标题】:How to prevent ajax request?如何防止ajax请求?
【发布时间】:2011-04-18 12:10:09
【问题描述】:

我有以下情况:页面显示一组项目,当用户点击项目时,通过 ajax 请求加载项目详细信息。我想要做的是防止进一步点击时的ajax请求,因为信息已经加载。

我使用 jQuery 和 Rails 3,我的代码如下所示:

# view
- @items.each do |item|
  .item
    = link_to item.name, item_path(item), :remote => true

// js
$(function(){
  $('.item').bind('ajax:success', function(xhr, data, status){
    ...
  });

  $('.item').bind('ajax:before', function(xhr){
    if (...) { // check if data already loaded
       // ???
    }
  });
});

我走对了吗?还是有其他解决方案?

【问题讨论】:

    标签: jquery ajax ruby-on-rails-3


    【解决方案1】:

    您可以在 ajax 调用之前设置像 itemsLoaded = false 这样的标志。加载详细信息的那一刻,您可以将该标志设置为itemsLoaded = true。 ajax 调用只应在if(!itemsLoaded){/* Do ajax to fetch details */}else return;

    时执行

    【讨论】:

      【解决方案2】:

      我认为你唯一能做的就是确保item中的点击只进入ajax函数一次。方法:

      • click 事件调用一个禁用绑定项的函数

      • 如果你想确定,你在ajax成功函数中做同样的事情

      • 单击项目后,您的函数会检查项目信息是否已经存在。如果是,那么它只是返回。

      【讨论】:

      • 是的,这是我想到的第一个解决方案,但是其中的代码太多,对我来说并不完美。我寻找“不引人注目”和“rails 3 way”的东西。
      【解决方案3】:

      一种解决方案是您可以创建一个布尔值,然后仅在该值为 false 时进行 ajax 调用。

      【讨论】:

        【解决方案4】:

        像这样在 Ajax:success 函数中设置一个变量...

        AjaxSuccessVar=0;
        $(function(){
            $('.item').bind('ajax:success', function(xhr, data, status){
               AjaxSuccessVar = 1;
        });
        
        $('.item').bind('ajax:before', function(xhr){
           if (AjaxSuccessVar==0) { // check if data already loaded
              //then do your ajax function here
           }
        });
        

        【讨论】:

          【解决方案5】:

          使用.data 属性作为指标怎么样,例如:

            $('.item').bind('ajax:success', function(xhr, data, status){
                $(this).data("loaded", true);
          
            }).bind('ajax:before', function(xhr){
                if (!$(this).data("loaded")) {
                    // data has not been loaded
                } else {
                    // do something
                }
            });
          

          编辑:

          一旦点击了“触发器”项,请尝试解除点击处理程序的绑定,例如:

          $(".item").click(function() {
              $.ajax({ ... });
              $(this).unbind("click");
          });
          

          或者在成功完成请求后ajax:success

            $('.item').bind('ajax:success', function(xhr, data, status){
                $(this).unbind("click");
            });
          

          【讨论】:

          • 我不需要指示器(我可以检查数据是否加载),我需要阻止ajax请求发送。
          • @Dmitry Maksimov - 啊,对不起,我以为这就是你想要的。我已经更新了我的答案,希望它现在更有帮助。
          • 感谢您的回复,但我找到了答案。
          【解决方案6】:

          原来我使用了错误的回调。正确的回调是ajax:beforeSend,带有以下签名function(event, xhr, settings)。为了防止发送请求,我使用了xhr.abort(),它工作正常。

          http://www.alfajango.com/blog/rails-3-remote-links-and-forms/

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2017-01-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-11-21
            相关资源
            最近更新 更多