WordPress – Ajax Request Keeps Returning 0

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’);

Read More

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();

Related posts

Leave a Reply

4 comments

  1. 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();
    
  2. if you write

    add_action('wp_ajax_nopriv_**get_ldapattr**', '**get_ldap_attr**');
    

    so you have to call get_ldapattr and not get_ldap_att.

    In other words, if you specify

    add_action('wp_ajax_nopriv_**ACTION_HANDLER**', '**CALLBACK_FUNCTION**');
    

    you have to call ACTION_HANDLER in your ajax js script (unless ACTION_HANDLER coincides with CALLBACK_FUNCTION)

  3. that’s because it returns results from conditional that checks if user is logged as admin.
    0 is false.

    Update:
    Try instead of return on end of the function, to put echo or print results. I think you end function procedure, but other processes from wp keep getting still.

  4. You’re missing the “header” section.
    Your PHP script should end like this:

        header( 'Content-Type: application/json' ); // <<------
        echo json_encode($sup);
        die();