Php AJAX调用返回状态200,但不返回内容
我有一个简单的AJAX调用,它从文件中检索文本,将其推送到表中,然后显示它。在运行Apache2.2.26/PHP5.3的Mac和运行Apache2.2.1.6/PHP5.3的Ubuntu设备上进行测试时,调用可以正常工作。它在运行Apache2.2.4/PHP5.1的RedHat上不起作用。当然,红帽盒是我唯一需要它工作的地方 该调用返回200 OK,但没有内容。即使在文件中找不到任何内容(或者无法访问),也会回显表头,因此如果权限有问题,我仍然希望看到一些内容。但可以肯定的是,我验证了该文件是所有用户都可读的 代码已被编辑和简化 我的ajax功能:Php AJAX调用返回状态200,但不返回内容,php,ajax,Php,Ajax,我有一个简单的AJAX调用,它从文件中检索文本,将其推送到表中,然后显示它。在运行Apache2.2.26/PHP5.3的Mac和运行Apache2.2.1.6/PHP5.3的Ubuntu设备上进行测试时,调用可以正常工作。它在运行Apache2.2.4/PHP5.1的RedHat上不起作用。当然,红帽盒是我唯一需要它工作的地方 该调用返回200 OK,但没有内容。即使在文件中找不到任何内容(或者无法访问),也会回显表头,因此如果权限有问题,我仍然希望看到一些内容。但可以肯定的是,我验证了该文件
function ajax(page,targetElement,ajaxFunction,getValues)
{
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState===4 && xmlhttp.status===200)
{
document.getElementById(targetElement).innerHTML=xmlhttp.responseText;
}
};
xmlhttp.open('GET','/appdir/dir/filedir/'+page+'_funcs.php?function='+ajaxFunction+'&'+getValues+'&'+new Date().getTime(),false);
xmlhttp.setRequestHeader('cache-control','no-cache');
xmlhttp.send();
}
我这样称呼它:
ajax('pagename','destelement','load_info');
并返回结果:
// Custom file handler
function warn_error($errno, $errstr) {
// Common function for warning-prone functions
throw new Exception($errstr, $errno);
}
function get_file_contents() {
// File operation failure would return a warning
// So handle specially to suppress the default message
set_error_handler('warn_error');
try
{
$fh = fopen(dirname(dirname(__FILE__))."/datafile.txt","r");
}
catch (Exception $e)
{
// Craft a nice-looking error message and get out of here
$info = "<tr><td class=\"center\" colspan=\"9\"><b>Fatal Error: </b>Could not load customer data.</td></tr>";
restore_error_handler();
return $info;
}
restore_error_handler();
// Got the file so get and return its contents
while (!feof($fh))
{
$line = fgets($fh);
// Be sure to avoid empty lines in our array
if (!empty($line))
{
$info[] = explode(",",$line);
}
}
fclose($fh);
return $info;
}
function load_info() {
// Start the table
$content .= "<table>
<th>Head1</th>
<th>Head2</th>
<th>Head3</th>
<th>Head4</th>";
// Get the data
// Returns all contents in an array if successful,
// Returns an error string if it fails
$info = get_file_contents();
if (!is_array($info))
{
// String was returned because of an error
echo $content.$info;
exit();
}
// Got valid data array, so loop through it to build the table
foreach ($info as $detail)
{
list($field1,$field2,$field3,$field4) = $detail;
$content .= "<tr>
<td>$field1</td>
<td>$field2</td>
<td>$field3</td>
<td>$field4</td>
</tr>";
}
$content .= "</table>";
echo $content;
}
//自定义文件处理程序
函数warn\u error($errno,$errstr){
//常见的警告函数
抛出新异常($errstr,$errno);
}
函数get_file_contents(){
//文件操作失败将返回警告
//所以要特别处理以抑制默认消息
设置错误处理程序(“警告错误”);
尝试
{
$fh=fopen(dirname(dirname(__文件))。“/datafile.txt”,“r”);
}
捕获(例外$e)
{
//制作一个漂亮的错误消息,然后离开这里
$info=“致命错误:无法加载客户数据。”;
还原错误处理程序();
返回$info;
}
还原错误处理程序();
//已获取文件,因此获取并返回其内容
而(!feof($fh))
{
$line=fgets($fh);
//请务必避免数组中出现空行
如果(!空($line))
{
$info[]=分解(“,”,$line);
}
}
fclose($fh);
返回$info;
}
函数加载_info(){
//开桌
$content.=”
标题1
标题2
总目3
总目4”;
//获取数据
//如果成功,则返回数组中的所有内容,
//如果失败,则返回错误字符串
$info=获取文件内容();
如果(!is_数组($info))
{
//由于出现错误,返回了字符串
echo$content.$info;
退出();
}
//得到了有效的数据数组,所以循环使用它来构建表
foreach($info作为$detail)
{
列表($field1、$field2、$field3、$field4)=$detail;
$content.=”
$field1
$field2
$field3
$field4
";
}
$content.=”;
echo$内容;
}
在它工作的地方,响应头将连接指示为保持活动状态;如果出现故障,则关闭连接。我不知道这是否重要
我在网上到处寻找线索,但“无内容”问题总是指向同源政策问题。在我的例子中,所有内容都在同一台服务器上
我不知道下一步该做什么/看哪里。
file\u get\u contents()
需要一个参数。它不知道你想要什么,所以它返回false。另外,您使用的get\u file\u contents()
顺序错误。这是PHP版本的问题。在load_info函数中,我使用了filter_输入(input_GET,“value”),但这在PHP5.1中不可用。我从我最初的代码帖子中删除了这个,因为我不认为这是问题的一部分。经验教训。$info=get_file_contents();-这是您编写的自定义函数吗?因为原始文件是file_get_contents(),它需要一个源…get_file_contents()实际上是一个自定义文件处理程序。可能没有用最好的名字。编辑我的帖子以包含处理程序。此函数是否返回内容?自定义文件处理程序在关闭文件后返回$info数组;load_info()函数没有——它只是回显html。