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!!");
}
})
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.
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.
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.
click(function(){ window. location. reload(); alert("Hello world"); });
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!!");
}
})
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