1

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

    });
});
  • 2
    What is the value of ajaxurl variable? Did you try hard-coding it? – montrealist Jul 24 '13 at 13:17
  • Surely that code can't be right? That Javascript block is in PHP context and would trigger errors. – s_ha_dum Jul 24 '13 at 13:32
  • No they aren't nested together. – shapada Jul 24 '13 at 13:38
  • Does it return anything at all, at any point? That is, echo something at the top of the script and see if it returns, then move down a couple of lines. Isolate where this fails. – s_ha_dum Jul 24 '13 at 13:40
  • @shapada : Please edit your code so that context is clear. – s_ha_dum Jul 24 '13 at 13:41
3

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.

  • I'm loading the file through a shortcode. The file is located in the /inc directory. – shapada Jul 24 '13 at 19:46
  • 1
    It worked! By moving the PHP into a plugin and the jQuery into the header my object was successfully passed. Could you please explain why!? Thanks you so much! You rock! – shapada Jul 24 '13 at 20:48
  • I don't really know what "loading the file through a shortcode" means, so let's just go with "your php function was not loaded when the actual AJAX call was being made, so your function never executed when you made the call to admin-ajax.php" as an explanation. – Otto Jul 25 '13 at 1:55
  • So I have it working in Chrome but the darn thing won't work in Firefox or IE. Any suggestions on why this might be? – shapada Jul 26 '13 at 18:16
0

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.

  • Just tried it and still 0 response. – shapada Jul 24 '13 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.