Stumped – Ajax Response Returns 0

I can’t seem to figure it out. No matter what I do, the ajax call keeps giving a 0 response. I’ve tested my PHP funtion and it works fine but whenever I call it through ajax I receive nothing (only a 0 response). Any help/suggestions is much appreciated.

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

    });
});

Related posts

2 comments

  1. Debug it logically.

    This code works. I just tested it. This in a theme (I put it in header.php for testing):

    jQuery(document).ready(function() {
        var lanid = 'demo';
        var ajaxurl = '<?php echo admin_url("admin-ajax.php", null); ?>';
        data = { action: "get_ldapattr", lanid: lanid};     
        jQuery.ajax({
            url: ajaxurl,
            data: data,
            dataType: 'json',
            type: 'post',
            success: function(response) {
            console.log(response);  
            }
        });
    });
    

    And this code in a plugin:

    add_action('wp_ajax_get_ldapattr', 'get_ldap_attr');
    add_action('wp_ajax_nopriv_get_ldapattr', 'get_ldap_attr');
    function get_ldap_attr() {
        $sup = "data is ".$_POST['lanid'];
        echo json_encode($sup);
        die();
    }
    

    What happens when I use both these pieces of code is that I get “data is demo” in the JS console. Which is what you would expect, basically.

    So now, you have to determine why this doesn’t work for you. I would specifically look at where you have the PHP code located. Is it in a plugin? Is it in the theme’s functions.php file? Are you sure that that part of the code is loaded at all for AJAX responses? It won’t work if you have the code in a Page Template, for example, because Page Templates don’t load when using the ajax handler.

  2. The following works for me on my local WordPress install. Note that I hardcoded the AJAX URL, it will be different depending on your environment.

    In theme’s functions.php:

    add_action('wp_ajax_get_ldapattr', 'get_ldap_attr');
    add_action('wp_ajax_nopriv_get_ldapattr', 'get_ldap_attr');
    
    function get_ldap_attr() {
        $lanid = $_POST['lanid'];
        echo json_encode($lanid);
        die();
    }
    

    In the page template itself (for example page.php):

    <input id="empLanId" name="empLanId" value="foo" />
    

    In JavaScript file being included on the page template above:

    jQuery('#empLanId').on('blur', function() {
        var ajaxurl = 'http://localhost/wordpress-vanilla-3.5.1/wp-admin/admin-ajax.php';
        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);
            }
        });
    });
    

    Note that I removed any logic that does not have anything to do with WordPress, in order to try isolate the issue. My conclusion is that your WordPress-related code should be fine.

Comments are closed.