如果成功一次,jQuery刷新/重新加载页面

我使用Jquery来调用Ajax。 服务器返回Json,其中包含两个值“true或false”,如下所示:

return Json(new { success = false, JsonRequestBehavior.AllowGet }); 

如果服务器返回true,有没有办法在5秒后刷新页面?

在你的ajax成功回调中这样做:

 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); } } 

由于您希望在5秒后重新加载页面,因此您需要按照答案中的建议进行超时。

  location.reload(); 

您可以在if条件中使用重新加载function以获得成功,并且在条件成功后将重新加载页面。

 if(success == true) { //For wait 5 seconds setTimeout(function() { location.reload(); //Refresh page }, 5000); } 
 var val = $.parseJSON(data); if(val.success == true) { setTimeout(function(){ location.reload(); }, 5000); } 
 $.ajax("youurl", function(data){ if (data.success == true) setTimeout(function(){window.location = window.location}, 5000); }) )