Php AJAX不返回LDAP响应,但在AJAX之外调用函数时可以工作

Php AJAX不返回LDAP响应,但在AJAX之外调用函数时可以工作,php,jquery,ajax,ldap,joomla3.0,Php,Jquery,Ajax,Ldap,Joomla3.0,我正在开发一个定制的Joomla模块,该模块返回一个LDAP目录,能够使用AJAX更改前端的排序选项 如果我在default.php模板文件中将目录作为字符串调用,则getAjax函数返回的目录很好(绕过AJAX): 问题是,当我试图通过ajax返回变量“$content”时,在更改选择器时,目录不会显示。但是,在helper.php中,如果我将“return$content”更改为“return$sortOption”,AJAX将工作并返回所选的排序选项。所以我知道AJAX正在发挥作用。还要注

我正在开发一个定制的Joomla模块,该模块返回一个LDAP目录,能够使用AJAX更改前端的排序选项

如果我在default.php模板文件中将目录作为字符串调用,则getAjax函数返回的目录很好(绕过AJAX):

问题是,当我试图通过ajax返回变量“$content”时,在更改选择器时,目录不会显示。但是,在helper.php中,如果我将“return$content”更改为“return$sortOption”,AJAX将工作并返回所选的排序选项。所以我知道AJAX正在发挥作用。还要注意,如果我更改为“return$content.$sortOption”,则会显示select选项变量,但不会显示目录。我认为这与LDAP没有通过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'));
//无法直接访问
定义的(“ujexec”)或模具;
//仅包含一次辛迪加职能
require_once(dirname(_文件)'/helper.php');
//实例化全局文档对象
$doc=JFactory::getDocument();
$js=get('dirOptions');
$directoryList=modNuDirectoryHelper::getAjax($dirDepts);
require(JModuleHelper::getLayoutPath('mod_-nu_目录');
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;
    }
}
类modNuDirectoryHelper{
公共静态函数getAjax($dirDepts){
//使用ajax从select字段获取排序变量:
$input=JFactory::getApplication()->input;
$sortOption=$input->get('data');
//设置我们的变量
$baseDN='CN=Users,DC=site,DC=local';
$adminDN=“admin”;
$adminPswd=”P@55WorD";
$ldap\u conn=ldap\u connect('ldaps://ad.site.local');
$dirFilter=strtolower('(|(department=*')。内爆('*)(department=*',$dirDepts)。'*));
//如果选择了“所有”类别,请不要添加筛选器,否则请添加目录筛选器
(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_选项($ldap_conn,ldap_OPT_PROTOCOL_VERSION,3);
$ldap\u bind=ldap\u bind($ldap\u conn、$adminDN、$adminPswd);
如果(!$ldap\U绑定){
返回“哦,不!无法连接到目录:(”;
}否则{
$attributes=数组('displayname','mail','telephonenumber','title','department','physicalDelivery','OfficeName','samaccountname','wwhomepage','sn','givenname');
$result=ldap\u search($ldap\u conn、$baseDN、$filter、$attributes);
//按姓氏对条目进行排序
ldap\u排序($ldap\u conn,$result,$sortOption);
$entries=ldap\u get\u条目($ldap\u conn,$result);
//让我们在目录中循环
对于($i=0;$i<$entries[“count”];$i++){
//为循环中的每个迭代定义变量
$userName=$entries[$i]['displayname'][0];
$userTitle=$entries[$i]['title'][0];
$userDept=$entries[$i]['department'][0];
$userPhone='888-888-8888,分机。$entries[$i]['telephonenumber'][0];
$userOffice='Office:'。$entries[$i]['physicaldeliveryofficename'][0];
//人员必须有姓名、职务和部门
如果((!empty($userName))| |(!empty($userTitle))| |(!empty($userDept))){
$content.=$userName.“
” .$userTitle.“
” .$userDept.“
” .(!empty($userPhone)?$userPhone:''。“
” .(!empty($userOffice)?$userOffice:''。“
” “
”; } } } 返回$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>

显示以下部门:

显示以下选项:

名字 姓 部门
问题在于$entries数组没有被视为实际数组。我已经通过用静态数组替换$entry数组进行了测试,AJAX回调行为正常。我删除了AJAX功能,只是回显了该函数,效果良好。但这并不能解决AJAX无法提取数组的原因。

不确定这些否决票是从哪里来的。突然我的每一篇帖子都被否决了。展示你自己!!!
<?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>