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

Anh*_*ang 13 ajax jquery

我使用Jquery来调用Ajax.服务器返回值为"true或false"的Json对象,如下所示:

return Json(new { success = false, JsonRequestBehavior.AllowGet });
Run Code Online (Sandbox Code Playgroud)

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

Jai*_*Jai 44

在你的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); 
   }
}
Run Code Online (Sandbox Code Playgroud)

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

  • @BalaKarthik,这个时间以毫秒为单位,等于5秒. (2认同)
  • 5000表示5000毫秒(1秒= 1000毫秒) (2认同)

小智 10

location.reload();
Run Code Online (Sandbox Code Playgroud)

您可以reload在if条件成功时使用该功能,并在条件成功后重新加载页面.


Sad*_*san 7

if(success == true)
{
  //For wait 5 seconds
  setTimeout(function() 
  {
    location.reload();  //Refresh page
  }, 5000);
}
Run Code Online (Sandbox Code Playgroud)