AJAX调用返回状态200,但没有内容 [英] AJAX call returns status 200 but no content
问题描述
我有一个简单的AJAX调用,从文件中检索的文本,它推到一个表,并显示。在Mac上运行的Apache 2.2.26 / PHP 5.3和运行Apache 2.2.1.6/PHP 5.3 Ubuntu的箱测试时,调用工作没有问题。运行Apache 2.2.4 / PHP 5.1在RedHat它不工作。当然,RedHat的箱子是唯一的地方,我需要它来工作。
I have a simple AJAX call that retrieves text from a file, pushes it into a table, and displays it. The call works without issue when testing on a Mac running Apache 2.2.26/PHP 5.3 and on an Ubuntu box running Apache 2.2.1.6/PHP 5.3. It does not work on RedHat running Apache 2.2.4/PHP 5.1. Naturally, the RedHat box is the only place where I need it to be working.
调用返回200 OK,但没有内容。即使没有在文件中找到的(或者它无法访问),表头是呼应因此,如果权限是一个问题,我仍然希望看到的东西。但可以肯定,我验证该文件是所有用户可读。
The call returns 200 OK but no content. Even if nothing is found in the file (or it's inaccessible), the table header is echoed so if permissions were a problem I would still expect to see something. But to be sure, I verified the file is readable by all users.
code经过编辑和简化。
Code has been redacted and simplified.
我的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();
}
我把它称为是这样的:
I call it like this:
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;
}
如果它的工作原理,响应头表示连接的保活;在那里失败,则连接被关闭。我不知道这是否重要。
Where it works, the response header indicates the connection as keep-alive; where it fails, the connection is closed. I don't know if that matters.
我过来,让并网的一些线索,但找遍了没有内容的问题不约而同地指向同源策略的问题。就我而言,所有的内容都是一样的服务器上。
I've looked all over SO and the net for some clues but "no content" issues invariably point to same-origin policy problems. In my case, all content is on the same server.
我在一个损失,因为做什么/在哪里寻找下一个。
I'm at a loss as to what to do/where to look next.
推荐答案
这竟然是一个PHP版本的问题。在load_info功能,我使用filter_input(INPUT_GET,值),但这是不具备的PHP 5.1。我把从我最初的code后,因为我不认为这是问题的一部分。经验教训。
This turned out to be a PHP version issue. In the load_info function I was using filter_input(INPUT_GET,"value"), but that was not available in PHP 5.1. I pulled that from my initial code post because I didn't think it was part of the problem. Lesson learned.
这篇关于AJAX调用返回状态200,但没有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!