Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

refresh page after jquery ajax

I am trying to refresh the page after a jQuery AJAX call. It appears that the page is refreshing (only sometimes) before all the data has been transmitted. Can anyone see why by this snippet below. I have the refresh code in the success function so I am confused.

I tried adding async = false and that didn't seem to work either.

function sendRating(rating, reload_on_return) {

$.ajax({
    type: "POST",
    dataType: 'json',
    url: window.url_root + cid + "/",
    data: {
        "rating": rating.r2 / 100.0
    },
    success: function(data) {
        if (data.hasOwnProperty('success')) {
            console.log("data was sent!");

            if (reload_on_return) {
                    location.reload();

            }

        }
    },
    error: function() {
        console.log("Data didn't get sent!!");
    }
})
like image 955
jaynp Avatar asked Aug 08 '13 16:08

jaynp


People also ask

How do you refresh a page after AJAX success?

The location. 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.

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.

How do you refresh a page using JavaScript?

You can use the location. reload() JavaScript method to reload the current URL. This method functions similarly to the browser's Refresh button. The reload() method is the main method responsible for page reloading.

How do I show message alerts after page reload?

click(function(){ window. location. reload(); alert("Hello world"); });


1 Answers

You could possibly do a setTimeout, to make it wait for a split second before the refresh can execute.

function sendRating(rating, reload_on_return) {

$.ajax({
    type: "POST",
    dataType: 'json',
    url: window.url_root + cid + "/",
    async: false,
    data: {
        "rating": rating.r2 / 100.0
    },
    success: function(data) {
        if (data.hasOwnProperty('success')) {
            console.log("data was sent!");

            if (reload_on_return) {
                setTimeout(
                  function() 
                  {
                     location.reload();
                  }, 0001);    
            }

        }
    },
    error: function() {
        console.log("Data didn't get sent!!");
    }
})
like image 200
broguyman Avatar answered Sep 28 '22 03:09

broguyman