Ajax 响应包含的内容超出预期
文件结构
js/scripts.jslibrary
/libary.phpmodules
/page.phpmodulus
/404.phpindex.php
文件说明
index.php看起来是什么路径部分url 是,并且需要该文件。所以www.test.com/page 将需要modules/page.php。一些代码:
$file = (DIR . 'modules/'.$action.'.php');
if(!file_exists($file))
{
$file = DIR . 'modules/404.php';
}
//Get file
require($file);
page.php 的主体如下所示:
<input type='submit' value='getTime' onClick='getTime()' />
JS 函数如下所示:
function updateStatus()
{
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
} else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("content").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","library/library.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("status=page");
}
库文件包含如下内容:
return time();
问题
问题是 reponseText 不只包含时间,它还包含 404.php 页面。这是因为我的index.php。他寻找 module/library.php,但它不存在(不在 module 目录中)。
那么我的问题是,ajax请求和响应的方式是什么?如何解决这个问题而不将library.php放在模块目录中?
我真正的问题有点复杂,但其根源与我在这里描述的问题相同。所以获得时间不是我的问题,这只是为了说明我的问题。
Filestructure
js/scripts.js
library/libary.php
modules/page.php
modulus/404.php
index.php
Explanation of the files
index.php looks what the path part of the url is, and require that file. So www.test.com/page will require modules/page.php. Some code:
$file = (DIR . 'modules/'.$action.'.php');
if(!file_exists($file))
{
$file = DIR . 'modules/404.php';
}
//Get file
require($file);
The body of page.php looks like this:
<input type='submit' value='getTime' onClick='getTime()' />
The JS function looks like this:
function updateStatus()
{
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
} else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("content").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","library/library.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("status=page");
}
And the library file contains something like this:
return time();
Problem
The problem is that the reponseText doesn't contain the time only, it also contains the 404.php page. This is because of my index.php. He looks for a modules/library.php, and it doensn't exists (not in de the modules dir).
So my question is, what is the way of an ajax request and response? And how do I solve this problem without putting the library.php in the modules dir?
My real problem is a bit more complex, but its in it roots the same problem as I described here. So getting the time is not my issue, its only a thing to illustrate my problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论