Ajax 响应包含的内容超出预期

发布于 2024-12-20 18:22:45 字数 1417 浏览 0 评论 0原文

文件结构

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文