Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Refresh/Reload Page if Ajax Success after time

Tags:

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?

like image 372
Anh Hoang Avatar asked Dec 26 '14 07:12

Anh Hoang


People also ask

How do you refresh a page after ajax success?

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.

Does ajax request reload page?

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.


2 Answers

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.

like image 127
Jai Avatar answered Nov 02 '22 07:11

Jai


location.reload();

You can use the reload function in your if condition for success and the page will reload after the condition is successful.

like image 34
Vikas Gautam Avatar answered Nov 02 '22 07:11

Vikas Gautam