food
food

Reputation: 31

AJAX call returns status 200 but no content

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.

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 has been redacted and simplified.

My ajax function:

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');

And return the results:

// 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.

Upvotes: 1

Views: 364

Answers (2)

food
food

Reputation: 31

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.

Upvotes: 1

Sterling Archer
Sterling Archer

Reputation: 22395

file_get_contents() expects a parameter. It does not know what you want, so it returned false. Also, you used get_file_contents() which is the wrong order.

Upvotes: 1

Related Questions