Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I reload the page after all ajax calls complete?

The first time a user is visiting my website, I am pulling a lot of information from various sources using a couple of ajax calls. How do I reload the page once the ajax calls are done?

if(userVisit != 1) {
  // First time visitor
  populateData();
}

function populateData() {
  $.ajax({
            url: "server.php",
            data: "action=prepare&myid=" + id,
            dataType: "json",
            success: function(json) {
                if(json.error) { 
                    return;
                }
                _id = response[json].id;
                getInformation(_id);
            }
  });
}

function getInformation(id) {
  $.ajax({
            url: "REMOTESERVICE",
            data: "action=get&id=" + id,
            dataType: "json",
            success: function(json) {
                if(json.error) { 
                    return;
                }

                $.ajax({
                       url: "server.php",
                       data: "action=update&myid=" + id + '&data=' + json.data.toString(),
                       dataType: "json",
                       success: function(json) {
                                if(json.error) { 
                                    return;
                                }
                      }
                });
            }
  });
}

So what the code does is, it gets a list of predefined identifiers for a new user (populateData function) and uses them to get more information from a thirdparty service (getInformation function). This getInformation function queries a third party server and once the server returns some data, it sends that data to my server through another ajax call. Now what I need is a way to figure out when all the ajax calls have been completed so that I can reload the page. Any suggestions?

like image 357
Legend Avatar asked Oct 26 '10 01:10

Legend


People also ask

How do I refresh a page after AJAX call?

You can use the location. reload() method to reload or refresh an entire web page or just the content inside an element. The . reload() method can be triggered either explicitly (with a button click) or automatically.

How do I return data after AJAX call success?

You can store your promise, you can pass it around, you can use it as an argument in function calls and you can return it from functions, but when you finally want to use your data that is returned by the AJAX call, you have to do it like this: promise. success(function (data) { alert(data); });

Does AJAX reload the page?

The AJAX help to refresh the whole page or particular area on the page. We can refresh div, table or button content etc. $. ajax({ type: 'POST', url: post_url, data: post_data, success: function(res) { $('#event').

How reload page after AJAX call in MVC?

So the page needs to refresh after an ajax call….. In the controller action build the redirect url and include any route parameters that are needed. The Url is returned in the Json object to the calling javascript along with any other values e.g. the result of a database update.


1 Answers

In your getInformation() call you can call location.reload() in your success callback, like this:

success: function(json) {
  if(!json.error) location.reload(true);
}

To wait until any further ajax calls complete, you can use the ajaxStop event, like this:

success: function(json) {
  if(json.error) return;
  //fire off other ajax calls
  $(document).ajaxStop(function() { location.reload(true); });
}
like image 141
Nick Craver Avatar answered Oct 31 '22 03:10

Nick Craver