【发布时间】:2015-02-23 14:22:08
【问题描述】:
我使用 Jquery 发出 Ajax 请求。服务器返回值为“true or false”的 Json 对象,如下所示:
return Json(new { success = false, JsonRequestBehavior.AllowGet });
如果服务器返回true,有什么方法可以在5秒后刷新页面?
【问题讨论】:
我使用 Jquery 发出 Ajax 请求。服务器返回值为“true or false”的 Json 对象,如下所示:
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 条件中使用reload 功能成功,条件成功后页面将重新加载。
【讨论】:
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);
}
【讨论】:
我更喜欢这种方式
使用ajaxStop + setInterval, 这将在同一页面中的任何 XHR[ajax] 请求后刷新页面
$(document).ajaxStop(function() {
setInterval(function() {
location.reload();
}, 3000);
});
【讨论】:
if statement 来检查值是否改变了
$.ajax("youurl", function(data){
if (data.success == true)
setTimeout(function(){window.location = window.location}, 5000);
})
)
【讨论】:
这里有很多很好的答案,只是出于好奇,今天研究了一下,使用setInterval而不是setTimeout不是最好吗?
setInterval(function() {
location.reload();
}, 30000);
让我知道你的想法。
【讨论】:
随便用
$.ajax({
success: function (response) {
location.reload();
},
error : function(xhr,errmsg,err) {
console.log(xhr.status + ": " + xhr.responseText); // provide a bit more info about the error to the console
}
});
【讨论】: