我似乎想不出来。无论我做什么,ajax调用都会不断给出0响应。我已经测试了我的PHP功能,它工作得很好,但每当我通过ajax调用它时,我都没有收到任何响应(只有0响应)。非常感谢您的帮助/建议。
add_action(\'wp_ajax_get_ldapattr\', \'get_ldap_attr\');
add_action(\'wp_ajax_nopriv_get_ldapattr\', \'get_ldap_attr\');
function get_ldap_attr() {
header("Content-type: application/json");
$lanid = $_POST[\'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[\'manager\'][0], \',\' );
// find position of first comma in CN=Mxxxxxx,OU=Users,OU=MCR,DC=mfad,DC=mfroot,DC=org (directReports field)
$eq = stripos ( $info[\'manager\'][0], \'=\' );
// find position of first =
$s_lanid = substr ( $info[\'manager\'][0], $eq+1, ( ( $comm-1 ) - ( $eq ) ) );
//get substring between = and comma...
$sup = getLDAPInfo ( $s_lanid, $bind, $ad, $dn );
// get supervisor\'s info...
}
echo json_encode($sup);
die();
}
jQuery(document).ready(function() {
jQuery(\'#empLanId\').on(\'blur\', function() {
var lanid = jQuery(\'#empLanId\').val(),
data = { action: "get_ldapattr", lanid: lanid};
jQuery.ajax({
url: ajaxurl,
data: data,
dataType: \'json\',
type: \'post\',
success: function(response) {
console.log(response);
}
});
});
});