我有一個簡單的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盒子是我需要它工作的唯一地方。AJAX調用返回狀態200,但沒有內容
該調用返回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;
}
如果它的工作原理,響應頭表示連接保持活力;失敗的地方,連接關閉。我不知道這是否重要。
我看過遍及SO和網絡的一些線索,但「無內容」問題總是指向相同來源的政策問題。就我而言,所有內容都在同一臺服務器上。
我不知道該做什麼/下一步要看哪裏。
$ info = get_file_contents(); - 你寫的是一個自定義函數嗎?因爲原始文件是file_get_contents(),並且它期望源... – mariobgr 2014-09-12 15:40:48
get_file_contents()的確是一個自定義文件處理程序。可能沒有使用最好的名字。編輯我的帖子,包括處理程序。 – food 2014-09-12 16:02:19
做這個功能返回內容? – mariobgr 2014-09-12 16:08:09