字preSS - Ajax请求保持返回0 [英] WordPress - Ajax Request Keeps Returning 0

查看:172
本文介绍了字preSS - Ajax请求保持返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法找出原因,没有什么是从我的AJAX调用返回(返回0)。当用户填写自己的网络ID的形式,其主管的信息自动填充了几场我正在试图做的是。任何帮助/建议是多AP preciated。这是我的code:

add_action('wp_ajax_nopriv_get_ldapattr','get_ldap_attr');

jQuery的(功能(){

 的jQuery(#empLanId')。在('模糊',函数(){

    VAR lanid =的jQuery('#empLanId)。VAL()
        数据= {行动:get_ldap_attr,lanid:lanid};

        jQuery.ajax({
            网址:ajaxurl,
            数据类型:JSON,
            数据:数据,
            成功:函数(响应){
                的console.log(响应);
            },
            错误:函数(){
                的console.log('错误');
            }

        });

});
 

});

功能get_ldap_attr($ lanid){

  $ DN = get_site_option(ldapServerOU);
$ USR = get_site_option(ldapServerCN);
$ PW = get_site_option(ldapServerPass);
$ ADDR = get_site_option(ldapServerAddr);
$ IDS =阵列();
$ AD = ldap_connect($地址)
    或死亡(连接错误。);
ldap_set_option($广告,LDAP_OPT_PROTOCOL_VERSION,3);
ldap_set_option($广告,LDAP_OPT_REFERRALS,0);
$绑定=的ldap_bind($广告,$ USR,$ PW);

如果($绑定){
    $ SearchFor =CN =$ lanid。
        $结果= ldap_search($广告,$ DN,$ SearchFor);

        $进入= ldap_first_entry($广告,$结果);
        如果($入境!= FALSE){
            $信息= ldap_get_attributes($广告,$输入);
        }
        $通讯= stripos函数($信息['directReports'],,);
            //找到CN = Mxxxxxx第一个逗号的位置,OU =用户,OU = MCR,DC = mfad,DC = mfroot,DC =组织(directReports场)
        $ EQ = stripos函数($信息['directReports'],'=');
            //找到第一的位置=
        $ s_lanid = SUBSTR($信息['directReports'],$当量+ 1,(($ COMM-1) - ($当量)));
            //获取=和逗号之间的子......为lanid幸福..
        $ SUP = getLDAP($ s_lanid,$广告,$ DN,$ USR,$ PW);
            //得到上司的信息...
}

//返回$燮;

回声json_en code($ SUP);死();
 

解决方案

如果你的 $ SUP 变量是有效的,填充了网站的选项检索和分析后,你需要实际上回声 JSON - 不会返回它

例在code:

 返回json_en code($ SUP);死();
 

..应该阅读:

 回声json_en code($ SUP);
死();
 

I can't seem to figure out why nothing is returned from my ajax call (returns a 0). What I'm trying to do is when a user fills out their LAN ID on a form, their supervisor's information auto populates a few fields. Any help/suggestions is much appreciated. Here's my code:

add_action('wp_ajax_nopriv_get_ldapattr', 'get_ldap_attr');

jQuery(function() {

jQuery('#empLanId').on('blur', function() {

    var lanid = jQuery('#empLanId').val(),
        data = { action: "get_ldap_attr", lanid: lanid };

        jQuery.ajax({
            url: ajaxurl,
            dataType: 'json',
            data: data,
            success: function(response) {
                console.log(response);
            },
            error: function() {
                console.log('error');
            }

        });

});

});

function get_ldap_attr($lanid) {

$dn = get_site_option ( "ldapServerOU" );
$usr = get_site_option ( "ldapServerCN" );
$pw = get_site_option ( "ldapServerPass" );
$addr = get_site_option ( "ldapServerAddr" );
$ids = array();
$ad = ldap_connect ( $addr )
    or die ( "Connection error." );
ldap_set_option ( $ad, LDAP_OPT_PROTOCOL_VERSION, 3 );
ldap_set_option ( $ad, LDAP_OPT_REFERRALS, 0 );
$bind = ldap_bind ( $ad, $usr, $pw );

if ( $bind ) {
    $SearchFor ="cn=".$lanid;
        $result = ldap_search ( $ad,$dn,$SearchFor );

        $entry = ldap_first_entry ( $ad, $result );
        if ( $entry != false )  {
            $info = ldap_get_attributes ( $ad, $entry );
        }
        $comm  = stripos ( $info['directReports'], ',' );
            // find position of first comma in CN=Mxxxxxx,OU=Users,OU=MCR,DC=mfad,DC=mfroot,DC=org  (directReports field)
        $eq = stripos ( $info['directReports'], '=' );
            // find position of first =
        $s_lanid = substr ( $info['directReports'], $eq+1, ( ( $comm-1 ) - ( $eq ) ) );
            //get substring between = and comma... for lanid happiness..
        $sup = getLDAP ( $s_lanid, $ad, $dn, $usr, $pw );
            // get supervisor's info...
}

//return $sup;

echo json_encode($sup); die();

解决方案

If your $sup variable is valid and populated after your site option retrieval and parsing, you need to actually echo out the JSON - not return it.

Example in your code:

return json_encode($sup); die();

..should read:

echo json_encode($sup);
die();

这篇关于字preSS - Ajax请求保持返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆