简体   繁体   中英

Page reloads but doesn't hit server

I have the following Ajax logon script. index.php will set a session and then return something like {"status":true,"msgs":[],"url":"\\/demo\\/administrator\\/index.php"} if the username and password checks out. The page then should reload, the PHP script will check if the session is set, and if so, will display the appropriate page.

"Sometimes" with FireFox 21.0 running on Windows 7, the page appears to reload, but then incorrectly re-displays the logon page. When I say "appears to reload", when using FireBug, I see the POST to the server, I then see the console.log "reload page" for a brief amount of time, and then the logon page is displayed. If I then manually reload the page, the session checks out, and the correct page is returned.

To troubleshoot, I put some syslog(LOG_INFO,"got here!"); in my PHP script, and I see it never got accessed a second time, thus my believe the server isn't getting hit after the reload. I've also checked the Apache access log, and I believe it only sees the first POST.

Can anyone let me know what is happening, and how to remedy it? Thank you

$.post('index.php',{task:'logon',username:username,password:password},
    function (data)
    {
        if(data.status==true){
            console.log('reload page');
            //window.location.href = data.url;
            window.location.href = window.location.href;
            //window.location.reload();
        }
        else {msgs.html("<ul>"+mkList(data.msgs)+"</ul>");}
    },'json'
);

This answer was really provided by Brian Lacy and user1600124, but they only left comments and didn't post this answer. So, please vote their comments up if you think this is a good answer.

Use window.location.reload(true) to submit data to server

If you don't explicitly tell the browser not to cache pages in the headers.. some browsers will still cache dynamic pages because the parameter that you send is the same.

As an alternate solution, you can append a timestamp to the url that would force browser to get content from server again.

also setting the pragma "no-cache" header for your page could help.

http://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Avoiding_caching

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM