我似乎无法弄清楚为什么我的 ajax 调用没有返回任何内容(返回 0)。我想要做的是,当用户在表单上填写他们的 LAN ID 时,他们的主管信息会自动填充几个字段。非常感谢任何帮助/建议。这是我的代码:
add_action('wp_ajax_nopriv_get_ldapattr', 'get_ldap_attr');
jQuery(函数(){
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');
}
});
});
});
函数 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();