php ajax成功后重新加载页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7881089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:33:38  来源:igfitidea点击:

Reload the page after ajax success

phpjqueryajaxrefreshreload

提问by bosniamaj

I have some problems with redirecting/reloading after a successful ajax call. Here is the situation:

在成功调用 ajax 后,我在重定向/重新加载时遇到了一些问题。这是情况:

I have item for deletion saved in an array. When I click on a button it calls for PHP file via ajax, and after success I need to reload the page. But I have some problem doing this. I searched the internet and couldn't find a working solution.

我将要删除的项目保存在数组中。当我单击一个按钮时,它会通过 ajax 调用 PHP 文件,成功后我需要重新加载页面。但是我这样做有一些问题。我在互联网上搜索并找不到可行的解决方案。

I have PHP file which goes through the array deleting item by item from the database.

我有一个 PHP 文件,它通过数组从数据库中逐项删除。

foreach($arrayVals as $key=>$val)
{
    //bla bla
}

Also, I have jQuery part:

另外,我有 jQuery 部分:

$("#button").live("click",function(){
    $.ajax({
        url, data, type... not important
        success: function(html){
                    location.reload();
                }
    });
});

I mean, the code works, but not good. It does delete items, but not all of them and then it reloads the page. Like, if I have 10 items to delete, it deletes like 6-7, and 3-4 items stay undeleted.

我的意思是,代码有效,但不好。它确实删除了项目,但不是全部,然后它重新加载页面。就像,如果我有 10 个要删除的项目,它会删除 6-7 个,而 3-4 个项目保持未删除状态。

It acts like it reloads the page too soon, like PHP file does not have enough time to process everything :D

它就像过早地重新加载页面一样,就像 PHP 文件没有足够的时间来处理所有内容:D

回答by Vikas Naranje

BrixenDK is right.

BrixenDK 是对的。

.ajaxStop()callback executed when all ajax call completed. This is a best place to put your handler.

.ajaxStop()回调在所有 ajax 调用完成时执行。这是放置处理程序的最佳位置。

$(document).ajaxStop(function(){
    window.location.reload();
});

回答by brixenDK

You use the ajaxStop to execute code when the ajax are completed:

当 ajax 完成时,您使用 ajaxStop 执行代码:

$(document).ajaxStop(function(){
  setTimeout("window.location = 'otherpage.html'",100);
});

回答by naveen kumar

use this Reload page

使用这个重新加载页面

success: function(data){
   if(data.success == true){ // if true (1)
      setTimeout(function(){// wait for 5 secs(2)
           location.reload(); // then reload the page.(3)
      }, 5000); 
   }
}