admin管理员组

文章数量:1415467

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']);

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']);
Share Improve this question edited Oct 18, 2016 at 7:26 anon asked Oct 18, 2016 at 7:12 anonanon 31 gold badge1 silver badge6 bronze badges 7
  • use a flag session for this status – Suraj Commented Oct 18, 2016 at 7:21
  • I see a } in more at the line 15 of 'update.php'. Check it please – The Dude Commented Oct 18, 2016 at 7:24
  • alert(data); what data are you getting in alert? – Kamran Khatti Commented Oct 18, 2016 at 7:29
  • echo message alert – anon Commented Oct 18, 2016 at 7:38
  • I mean it alerts 'Successful' or 'Not enough' ? – Kamran Khatti Commented Oct 18, 2016 at 7:44
 |  Show 2 more ments

5 Answers 5

Reset to default 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./files/location-location/location-location.html

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

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

The source is from here

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;
            }

Try this:

location.reload(true);

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.

本文标签: javascriptJquery refresh the page after successStack Overflow