I use Jquery to make an Ajax request. The server returns Json object with value "true or false" like this:
return Json(new { success = false, JsonRequestBehavior.AllowGet });
Is there any way to refresh page after 5 seconds if the server returns true?
reload() method, will reload (or refresh) an entire web page after the Ajax has performed its operation, that is, extracted data from an xml file. Therefore, I have added location. reload() inside the success callback function.
AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page. Classic web pages, (which do not use AJAX) must reload the entire page if the content should change.
In your ajax success callback do this:
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);
}
}
As you want to reload the page after 5 seconds, then you need to have a timeout as suggested in the answer.
location.reload();
You can use the reload
function in your if
condition for success and the page will reload after the condition is successful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With