AJAX-使用一个PHP函数返回两个JSON对象

时间:2013-07-26 作者:shapada

如何使用一个AJAX调用和PHP函数返回两个JSON对象?非常感谢您的帮助!

下面是PHP函数:

function get_ldap_attr() {
    header("Content-type: application/json");
    $lan = $_POST[\'lan\'];
    $dn = get_site_option ( "ldapServerOU" );
    $usr = get_site_option ( "ldapServerCN" );
    $pw = get_site_option ( "ldapServerPass" );
    $addr = get_site_option ( "ldapServerAddr" );
    $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=".$lan;
            $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... for lanid happiness..
            $sup = getLDAPInfo ( $s_lanid, $bind, $ad, $dn ); 
                // get supervisor\'s info...
    }

    echo json_encode($sup);
    die();  
}
以及jQuery:

jQuery(function() {
    jQuery(\'#empLanId\').on(\'blur\', function() {
        var lan = jQuery(\'#empLanId\').val();
        var ajaxurl = \'<?php echo admin_url("admin-ajax.php", null); ?>\';
        var data = { action: "get_ldap", lan: lan};
        jQuery.ajax({
            type: \'POST\',
            url: ajaxurl,
            data: data,
            dataType: \'json\',
            success: function(response) {
                jQuery(\'#empSupLanId\').val(response.lanid);
                jQuery(\'#empSupName\').val(response.fullname);
                jQuery(\'#empSupNumber\').val(response.phone);
            }
        });
    });
});

3 个回复
最合适的回答,由SO网友:M-R 整理而成

如果我理解正确,您需要将它们包装在一个数组中并传递给Javascript。

function get_ldap_attr() {
    header("Content-type: application/json");
    ...

    // suppose you want to return $second_var
    echo json_encode(array($sup,$second_var));
    die();  
}
JSON请求的成功函数将以数组的形式接收它们。您可以使用整数索引访问它们。

jQuery(function() {
    jQuery(\'#empLanId\').on(\'blur\', function() {
        var lan = jQuery(\'#empLanId\').val();
        var ajaxurl = \'<?php echo admin_url("admin-ajax.php", null); ?>\';
        var data = { action: "get_ldap", lan: lan};
        jQuery.ajax({
            type: \'POST\',
            url: ajaxurl,
            data: data,
            dataType: \'json\',
            success: function(response) {
                response[0] // first $sup variable
                response[1] // second $second_var variable
                //jQuery(\'#empSupLanId\').val(response.lanid);
                //jQuery(\'#empSupName\').val(response.fullname);
                //jQuery(\'#empSupNumber\').val(response.phone);
            }
        });
    });
});

SO网友:Razon Komar Pal

jQuery Code:

(function ($) {
    \'use strict\';
    $(function () {
        var value_1 = 10;
        var value_2 = 20;
        $(\'.btn\').on(\'click\', function(){
            $.ajax({
                url: \'your_ajax_url_here\',
                type: \'POST\',
                data: {
                    action  :   \'action_name\',
                    val_1   :   value_1,
                    val_2   :   value_2,
                },
                beforeSend: function () {
                    console.log(\'Sending....\');
                },
                success: function (response) {
                    var obj = JSON.parse(response);
                    console.log(obj);
                },
                error: function (errorThrown, status, error) {
                    console.log( status );
                }
            });
        })
    });
})(jQuery);

PHP Code:

function get_ldap_attr() {

   $obj_val_1 = $_POST[\'val_1\'];
   $obj_val_2 = $_POST[\'val_2\'];

   echo json_encode(array(
       \'obj_1\' => $obj_val_1,
       \'obj_2\' => $obj_val_2,
   ));

}
仅此而已。

SO网友:Ahdi Kaddes
<script type="text/javascript">
  $(document).ready(function(){

    //jquery script
    $("#emp_mat").change(function(){

      var emp_mat = $(this).val();
    $.ajax({
        url:"emp_info.php",
        dataType:\'json\',
        data:{data:emp_mat}
      }).done(function(result)
      {
        $("#nom_prenom").val(result[0]);
        $("#adresse").val(result[1]);

      })
    });

  });
</script>

<input type="text" id="nom_prenom"   disabled="true" size="58"/>
<input type="text" id="adresse"   disabled="true" size="58"/>
结束

相关推荐

如何将footer.php重置为其原始状态?(主题:折纸)

我不小心从页脚中删除了一些代码。php试图去掉“Theme By Origami”的页脚,现在页脚全乱了。如何将其重置为原始状态?我觉得自己太笨了,我应该在删除之前备份一下,我太粗心了。。我的网站:http://www.agnesphotoandfilm.co.uk/我的主题:折纸请帮忙