AJAX不返回LDAP响应,但在AJAX之外调用函数时有效

I am working on a custom Joomla module that returns an LDAP directory with the ability to change sort options on the front end using AJAX.

The getAjax function returns the directory just fine if I call it as a string in the default.php template file (bypassing AJAX):

echo $directoryList;

The problem is when I try to return the variable "$content" through ajax, the directory does not show when changing the selector. However, in the helper.php if I change "return $content" to "return $sortOption", AJAX works and returns the selected option for the sort. So I know AJAX is working. Also note that if I change to "return $content.$sortOption", the select option variable is shown but no directory. I think it has something to do with the LDAP not loading properly through AJAX.

Mod_nu_directory.php

// no direct access
defined('_JEXEC') or die;
// Include the syndicate functions only once
require_once( dirname(__FILE__) . '/helper.php' );

// Instantiate global document object
$doc = JFactory::getDocument();
$js = <<<JS
(function ($) {
    $(document).on('change', '#sortDir select', function () {        
        var value = $('#sortDir option:selected').val(),
            request = {
            'option' : 'com_ajax',
            'module' : 'nu_directory',
            'data' : value,
            'format' : 'raw'
        };
        $.ajax({
            type : 'POST',
            data : request,
            success: function (response) {
            $('.status').html(response);
            }
        });
        return false;
    });
})(jQuery)
JS;
$doc->addScriptDeclaration($js);


$dirDepts = $params->get('dirDepts', 'All');
$dirOptions = $params->get('dirOptions');
$directoryList = modNuDirectoryHelper::getAjax($dirDepts);
require( JModuleHelper::getLayoutPath('mod_nu_directory'));

helper.php

class modNuDirectoryHelper {
    public static function getAjax($dirDepts) {

        //get the sort variable from the select field using ajax:
        $input = JFactory::getApplication()->input;
        $sortOption = $input->get('data');

        //Set our variables    
        $baseDN = 'CN=Users,DC=site,DC=local';
        $adminDN = "admin";
        $adminPswd = "P@55WorD";
        $ldap_conn = ldap_connect('ldaps://ad.site.local');

        $dirFilter = strtolower('(|(department=*' . implode('*)(department=*', $dirDepts) . '*))');

        //if "All" categories are selected, dont add a filter, else add a directory filter
        (strpos($dirFilter, 'all directory') !== false) ?
        $filter = '(&(objectClass=user)(|(memberof=CN=Faculty,CN=Users,DC=site,DC=local)(memberof=CN=Staff,CN=Users,DC=site,DC=local)))' : $filter = '(&(objectClass=user)(|(memberof=CN=Faculty,CN=Users,DC=site,DC=local)(memberof=CN=Staff,CN=Users,DC=site,DC=local))' . $dirFilter . ')';

        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
        $ldap_bind = ldap_bind($ldap_conn, $adminDN, $adminPswd);
        if (!$ldap_bind) {
            return 'Oh no! Unable to connect to the directory :(';
        } else {
            $attributes = array('displayname', 'mail', 'telephonenumber', 'title', 'department', 'physicalDelivery', 'OfficeName', 'samaccountname', 'wwwhomepage', 'sn', 'givenname');
            $result = ldap_search($ldap_conn, $baseDN, $filter, $attributes);
            //sort the entries by last name 
            ldap_sort($ldap_conn, $result, $sortOption);
            $entries = ldap_get_entries($ldap_conn, $result);


            // let's loop throught the directory
            for ($i = 0; $i < $entries["count"]; $i++) {

                // define the variables for each iteration within the loop
                $userName = $entries[$i]['displayname'][0];
                $userTitle = $entries[$i]['title'][0];
                $userDept = $entries[$i]['department'][0];
                $userPhone = '888-888-8888, ext. ' . $entries[$i]['telephonenumber'][0];
                $userOffice = 'Office: ' . $entries[$i]['physicaldeliveryofficename'][0];

                //person must have a name, title, and department
                if ((!empty($userName)) || (!empty($userTitle)) || (!empty($userDept))) {
                    $content .= $userName . '<br />'
                            . $userTitle . '<br />'
                            . $userDept . '<br />'
                            . (!empty($userPhone) ? $userPhone : '') . '<br />'
                            . (!empty($userOffice) ? $userOffice : '') . '<br />'
                            . '<br />';

                }

            }           

        }            

        return $content;
    }
}

default.php

<?php
// No direct access
defined('_JEXEC') or die;
?>

<p>Displaying the following departments:<br />
    <?php
    foreach ($dirDepts as $dirDept) {
        echo '[' . $dirDept . '] ';
    }
    ?>
</p>

<p class="dirOptions">Displaying the following Options:<br />

    <?php
    foreach ($dirOptions as $dirOption) {
        echo '[' . $dirOption . '] ';
    }
    ?>    
</p>

<?php
if (in_array('showSort', $dirOptions)) {
    ?>

    <form method="post" id="sortDir">
        <select  name="sortDir" >            
            <option value="displayname" selected="selected">First name</option>
            <option value="sn">Last name</option>
            <option value="department">Department</option>
        </select>        
    </form>  

<?php } ?>

<div class="status"></div>

The problem was the $entries array was not being treated as an actual array. I've tested this by substituting the $entry array with a static array and the AJAX callback behaved properly. I since removed the ajax functionality and just echoed the function and works fine. This is not solve why AJAX can't pull the array though.