Stumped – Ajax Response Returns 0

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.