【发布时间】:2016-06-17 08:27:37
【问题描述】:
我的网站结构如下:
public_html/
- index.php
- students.php
用户加载包含一个按钮的站点 (index.php)。单击此按钮时,AJAX 用于加载“students.php”并将其显示给用户(好像他们只是无缝地转到了不同的页面)。这样做时会运行以下 JavaScript:
var state = {'Page' : 'Students'};
history.pushState(state, null, "students");
这会在浏览器历史记录中添加一个状态,并导致浏览器 URL 显示“example.com/students”而不是“example.com”。但是,如果用户此时要刷新,由于文件夹“students”实际上并不存在,因此不会加载任何内容(404 Not Found)。
我的问题是,如何让用户浏览器实际显示“index.php”并自动将它们带到学生页面。换句话说,用户刷新“example.com/students”,实际发生的是用户被带到 index.php 文件,AJAX 自动将他们带到学生页面(好像他们真的刷新了页面)
注意:我知道我可以将 null 传递给“pushState()”中的 url 参数,但是这种 URL 行为是需要的,因为它允许用户快速跳转到一个页面(如果我可以让它工作的话)
通过AJAX显示学生页面的完整代码如下:
/**
* Display students screen.
*/
function displayStudents(createState) {
if(typeof(createState)==='undefined') {
createState = true;
}
$("#container").css("width", $( window ).width());
$("#container").css("position", "fixed");
$("#container").animate({marginLeft: "-100%"}, ANIMATION_SPEED);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
$("#container").css("margin-left", "100%");
$("#container").html(xmlhttp.responseText);
$("#container").animate({marginLeft: "0"}, ANIMATION_SPEED, null, function reset() {
$("#container").css("width", "100%");
$("#container").css("position", "relative");
});
if(createState) {
var state = {'Page' : 'Students'};
history.pushState(state, null, "students");
}
}
};
xmlhttp.open("GET", "students.php", true);
setTimeout(function() { xmlhttp.send(); }, ANIMATION_SPEED);
}
这里的大部分代码都是用于动画的。
【问题讨论】:
-
实际运行显示students.php的按钮是什么js?
-
您可以为此使用
.htaccess文件
标签: javascript php html ajax apache