从 ajax 加载的子页面重新加载页面 [英] Reload page from ajax-loaded subpage

查看:75
本文介绍了从 ajax 加载的子页面重新加载页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ajax 将 php 生成的内容加载到某些菜单单击的主 div 中.

I am using ajax to load php generated content into main div on some menu click.

我想要做的是具有某种重新加载功能 - 如果某些超时到期,我希望用户重新进行身份验证,我会在将内容加载到 div 之前通过单击菜单检查此功能.

What I would like to do is have some kind of reload functionality - if some timeout expires I want user to reauthenticate, I am checking this on menu click before the content is loaded into div.

如果我用 php 来做这件事标头(位置:登录");

If I do this with php's header("location:login");

我想它想在加载 div 内重定向,但我希望整个页面都重定向...

I guess it wants to redirect inside loading div, but I want the whole page to redirect...

推荐答案

你能做的最好的事情就是这样

The best you can do is some thing like this

在文件顶部

ob_start();

将其添加到 php 文件的末尾

Add this to the end of your php file

if($_SEESION['active']){
    ob_end_flush()
}else{
    ob_clean();
    echo "{Session_invalid}";
    ob_end_flush();
}

然后在你的 AJAX(XMLHttpRequest)

Then in your AJAX(XMLHttpRequest)

ajax = new XMLHttpRequest();
ajax.open("GET", "/folder/yourFile.php", true);
ajax.onreadystatechange = function(){
    if(readyState == 4){
        if(ajax.responseText == "{Session_invalid}"){
            document.location = "http://your.authenticate.url/"
        }else{
            mydiv.innerHTML = ajax.responceText
        }
    }
}
ajax.send();

请注意,我是根据您所说的构建此内容的,不要删除您的 ajax,只需将 if 检查添加到 AJAX 中

Please note i have built this on what you have said dont remove your ajax just add the if check into the AJAX

此外,此代码已构建,因此您可以将其添加到任何文件中,即使您没有制作 php 文件,因此它只是顶部和底部的编辑,但请确保在标签中

Allso this code has been built so you can add it into any file, even if you did not make the php file so its just top and bottom edits please make sure there in the tags though

这匹配user228395 点 1

This matches user228395 point 1

这篇关于从 ajax 加载的子页面重新加载页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆