gpt4 book ai didi

php - WordPress - Ajax 请求不断返回 0

转载 作者:行者123 更新时间:2023-12-01 08:03:54 26 4
gpt4 key购买 nike

我似乎无法弄清楚为什么我的 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();

最佳答案

如果您的 $sup 变量在站点选项检索和解析后有效并已填充,则您实际上需要 echo 输出 JSON -不返回。

代码中的示例:

return json_encode($sup); die();

..应该阅读:

echo json_encode($sup);
die();

关于php - WordPress - Ajax 请求不断返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17814298/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com