【问题标题】:AJAX call returns status 200 but no contentAJAX 调用返回状态 200 但没有内容
【发布时间】:2014-09-12 15:37:43
【问题描述】:

我有一个简单的 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;
}

在它起作用的地方,响应头指示连接保持活动状态;在失败的地方,连接被关闭。我不知道这是否重要。

我在 SO 和网络上查看了一些线索,但“无内容”问题总是指向同源政策问题。就我而言,所有内容都在同一台服务器上。

我不知道下一步该做什么/去哪里。

【问题讨论】:

  • $info = get_file_contents(); - 那是你写的自定义函数吗?因为原始文件是 file_get_contents() 并且它需要一个源...
  • get_file_contents() 确实是一个自定义文件处理程序。可能没有使用最好的名称。编辑了我的帖子以包含处理程序。
  • 这个函数是否返回内容?
  • 自定义文件处理程序在关闭文件后返回$info数组; load_info() 函数没有——它只是回显 html。

标签: php ajax


【解决方案1】:

file_get_contents() 需要一个参数。它不知道你想要什么,所以它返回 false。另外,您使用了get_file_contents(),这是错误的顺序。

【讨论】:

    【解决方案2】:

    原来是 PHP 版本问题。在 load_info 函数中,我使用了 filter_input(INPUT_GET,"value"),但这在 PHP 5.1 中不可用。我从我最初的代码帖子中删除了它,因为我认为这不是问题的一部分。经验教训。

    【讨论】:

      猜你喜欢
      • 2019-04-09
      • 1970-01-01
      • 2015-10-13
      • 1970-01-01
      • 2020-01-27
      • 1970-01-01
      • 1970-01-01
      • 2013-12-26
      • 2017-06-26
      相关资源
      最近更新 更多