-2

How do I refresh the page after ajax call success. The page doesn't refresh even thought I have put window.location.reload(true). It only displays the echo message but doesn't refresh it

$("#update-btn").click(function() {
    var token = document.getElementById('tokenAmount').value;; //Place the token here
    var master = currentUserID;
    var lastUser = lastNode.info.id;
    $.ajax({
        type : "POST",
        url : "update.php", 
        data : {'master': master,
            'token' : token,
            'user_id': lastUser 
        },
        success : function(data) {
            alert(data);
            window.location.reload(true);
        }
    }); 
});

update.php

$master=$_POST['master'];
$user = $_POST['user_id'];
$token = $_POST['token'];

if(condition)
{
    $result = $MySQLi_CON->query($secondToken);
    if($result == false)
        $response['msg'] = "Not Enough";
    else
    {    
        $response['msg'] = "Successful";
    }
}

else
{
    $response['msg'] = "Not enough";
}
echo json_encode($response['msg']);
anon
  • 3
  • 1
  • 1
  • 6

5 Answers5

1

.ajaxStop() callback executes when all AJAX call completed. This is a best place to put your handler.

$(document).ajaxStop(function(){
    window.location.reload();
});

The source is from here

Community
  • 1
  • 1
Blueblazer172
  • 588
  • 2
  • 15
  • 44
1

Please try below code :-

location.reload();

Edited :-

success : function(data) {
            alert(data);
            location.reload();
        }

Other ways :-

location.reload()
history.go(0)
location.href = location.href
location.href = location.pathname
location.replace(location.pathname)
location.reload(false)

Please have a look on below link :- There are 534 other ways to reload the page with JavaScript :-

http://www.phpied.com/files/location-location/location-location.html

Anand Systematix
  • 632
  • 5
  • 15
0

Try this:

location.reload(true);
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
438sunil
  • 188
  • 1
  • 2
  • 13
0

The next statement wont execute unless you click Ok on alert(). So use console.log(data) instead.

success : function(data) {
                console.log(data);
                window.location.href=window.location.href;
            }
Rishabh
  • 1,205
  • 1
  • 11
  • 20
0

In your ajax success callback do this:

success: function(response){
                setTimeout(function(){
                   location.reload()
                }, 5000);
    }

As you want to reload the page after 5 seconds, then you need to have a timeout as suggested in the answer.

Jamil Ahmed
  • 284
  • 3
  • 17
  • 3
    In your ajax success callback do this: success: function(response){ setTimeout(function(){ location.reload() }, 5000); } As you want to reload the page after 5 seconds, then you need to have a timeout as suggested in the answer. – Jamil Ahmed Feb 13 '20 at 06:05
  • Edit it into the post please – U13-Forward Feb 13 '20 at 06:08