AJAX - Restituire Due Oggetti JSON con Una Funzione PHP
                            26 lug 2013, 20:34:16
                        
                    
                            Visualizzazioni: 47.8K
                        
                    
                            
                            
                                Voti: 0
                            
                        
                    Come posso restituire due oggetti JSON con una singola chiamata AJAX e una funzione PHP? Qualsiasi aiuto è apprezzato!
Ecco la funzione 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 ( "Errore di connessione." );
    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], ',' );
                // trova la posizione della prima virgola in CN=Mxxxxxx,OU=Users,OU=MCR,DC=mfad,DC=mfroot,DC=org (campo directReports)
            $eq = stripos ( $info['manager'][0], '=' );
                // trova la posizione del primo =
            $s_lanid = substr ( $info['manager'][0], $eq+1, ( ( $comm-1 ) - ( $eq ) ) );
                // ottieni la sottostringa tra = e virgola... per la felicità del lanid..
            $sup = getLDAPInfo ( $s_lanid, $bind, $ad, $dn ); 
                // ottieni le informazioni del supervisore...
    }
    echo json_encode($sup);
    die();  
}
E il 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);
            }
        });
    });
});
                Tutte le risposte alla domanda
                
                3
                
            
        
                
                
                0
                
                
                    
                    
                    
                    
                        
                        
                            
                                
                        
                    
                    
                        
                            
                                
                                     
                                
                                
                                    
                                        
                                        
                                
                            
                            
                                
                            
                            
                    
                
            
        Se ho capito bene, devi incapsularli in un array e passarli a Javascript.
function get_ldap_attr() {
    header("Content-type: application/json");
    ...
    // supponiamo che tu voglia restituire $second_var
    echo json_encode(array($sup,$second_var));
    die();  
}
La funzione di successo della richiesta JSON li riceverà come array. Puoi accedervi con un indice intero.
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] // prima variabile $sup
                response[1] // seconda variabile $second_var
                //jQuery('#empSupLanId').val(response.lanid);
                //jQuery('#empSupName').val(response.fullname);
                //jQuery('#empSupNumber').val(response.phone);
            }
        });
    });
});
 
                                
                                        M-R
                                    
                                    
                                            2.62K
                                        
                                    
                                
                                26 lug 2013 20:56:00
                            
                        
                
                
                0
                
                
                    
                    
                        
                        
                            
                                
                        
                    
                    
                        
                            
                                
                                     
                                
                                
                                    
                                        
                                        
                                
                            
                            
                                
                            
                            
                    
                
            
        Codice jQuery:
(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('Invio in corso....');
                },
                success: function (response) {
                    var obj = JSON.parse(response);
                    console.log(obj);
                },
                error: function (errorThrown, status, error) {
                    console.log( status );
                }
            });
        })
    });
})(jQuery);
Codice PHP:
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,
   ));
}
È tutto.
 
                                
                                        Razon Komar Pal
                                    
                                    
                                            316
                                        
                                    
                                
                                7 nov 2018 07:45:54
                            
                        
                
                
                1
                
                
                    
                    
                        
                        
                            
                                
                        
                    
                    
                        
                            
                                
                                     
                                
                                
                                    
                                        
                                        
                                
                            
                            
                                
                            
                            
                    
                
            
        <script type="text/javascript">
  $(document).ready(function(){
    //script jQuery
    $("#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"/>
 
                                
                                        Ahdi Kaddes
                                    
                                    
                                            9
                                        
                                    
                                
                                26 lug 2018 11:57:59
                            
                        
                    Domande correlate
                
                
                                        5
                                    
                                    
                                        risposte