【问题标题】:Refresh Page after AJAX - sometimes failsAJAX 后刷新页面 - 有时会失败
【发布时间】:2014-07-07 15:52:15
【问题描述】:

一旦 ajax 完成,我将使用它来刷新/重新加载页面。

$(".method").click( function() {
    if (confirm('Do  : ' + $(this).attr('method') + ' ?')) {
        $.ajax({ url : 'go.php?method=' + $(this).attr('method'),
        success: function(){
           setTimeout(function(){
           window.location = "?method=view";
            }, 100);
        }
   });
}
    return false;
});

有时它可以工作并且页面会刷新,有时 ajax 部分可以工作但页面不会刷新。

go.php 总是被调用并正确运行。

有什么办法让它一直工作吗?

谢谢

【问题讨论】:

    标签: jquery ajax refresh


    【解决方案1】:

    我相信在 ajax 过程中发生了错误。 Go.php 将始终被调用,但返回给浏览器的响应显然会有所不同。您可以指定一个 ERROR 函数并执行与成功相同的操作,但您可能希望显示一条错误消息。

    $(".method").click( function() {
        if (confirm('Do  : ' + $(this).attr('method') + ' ?')) {
            $.ajax({ url : 'go.php?method=' + $(this).attr('method'),
                success: function(){
                    setTimeout(function(){
                        window.location = "?method=view";
                    }, 100);
                },
                error: function(){
                    setTimeout(function(){
                        window.location = "?method=view";
                    }, 100);
                }
            });
        }
        return false;
    });
    

    或者,如果您不关心成功或错误,您可以使用 complete,这对两者都一样。

    $(".method").click( function() {
        if (confirm('Do  : ' + $(this).attr('method') + ' ?')) {
            $.ajax({ url : 'go.php?method=' + $(this).attr('method'),
                complete: function(){
                    setTimeout(function(){
                        window.location = "?method=view";
                    }, 100);
                }
            });
        }
        return false;
    });
    

    【讨论】:

      【解决方案2】:

      尝试使用此代码:

      $(".method").click( function() {
          if (confirm('Do  : ' + $(this).attr('method') + ' ?')) {
              $.ajax({
              url : 'go.php?method=' + $(this).attr('method'),
              success: function(){
              },
              complete: function() {
                 setTimeout(function(){
                 window.location.href = "?method=view";
                 }, 100);
      
              }
         });
      }
          return false;
      });
      

      参考:https://api.jquery.com/jQuery.ajax/complete 部分)

      【讨论】:

      • 我试过这个,我仍然有同样的问题。我会再次检查并报告。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-18
      • 2023-03-27
      • 1970-01-01
      • 2012-09-21
      • 2014-07-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多