我有一个简单的AJAX调用,它从文件中检索文本,将其推送到表中并显示它。在运行Apache 2.2.26 / PHP 5.3的Mac和运行Apache 2.2.1.6/PHP 5.3的Ubuntu盒子上进行测试时,调用可以正常运行。它不适用于运行Apache 2.2.4 / PHP 5.1的RedHat。当然,RedHat盒子是我需要它工作的唯一地方。
调用返回200 OK但没有内容。即使在文件中找不到任何内容(或者它无法访问),表头也会被回显,所以如果权限是一个问题我仍然希望看到一些东西。但可以肯定的是,我确认所有用户都可以读取该文件。
代码已经过编辑和简化。
我的ajax功能:
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;
}
在它工作的地方,响应头表示连接为keep-alive;失败的地方,连接关闭。我不知道这是否重要。
我看了SO和网络上的一些线索,但“没有内容”的问题总是指向同源政策问题。就我而言,所有内容都在同一台服务器上。
我不知道下一步该做什么/在哪里看。
答案 0 :(得分:1)
file_get_contents()
需要一个参数。它不知道你想要什么,所以它返回false。另外,您使用了get_file_contents()
这是错误的顺序。
答案 1 :(得分:1)
这被证明是PHP版本问题。在load_info函数中,我使用了filter_input(INPUT_GET,“value”),但这在PHP 5.1中不可用。我从最初的代码帖子中删除了它,因为我认为这不是问题的一部分。经验教训。