我已经能够从活动目录中使用ajax调用获取数据。用于从活动目录获取数据的php文件如下:
<?php
$username = 'maxxxxxxx';
$password = 'xxxxxxxxx';
$server = 'ldap://xxxxxxx';
$domain = '@asia.xxxxxx.com';
$port = 389;
$ldap_connection = ldap_connect($server, $port);
if (! $ldap_connection)
{
echo '<p>LDAP SERVER CONNECTION FAILED</p>';
exit;
}
// Help talking to AD
ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, 0);
$ldap_bind = @ldap_bind($ldap_connection, $username.$domain, $password);
if (! $ldap_bind)
{
echo '<p>LDAP BINDING FAILED</p>';
exit;
}
else
{
echo 'login successful';
}
$base_dn = "OU=Employees,OU=Accounts,OU=India,DC=asia,DC=xxxxx,DC=com";
$dispname="Mark Hwett";
$filter ="(&(objectClass=user)(displayName=$dispname))";
$attr = array("sn","givenname","employeeid","distinguishedname","displayname","samaccountName","department","manager","mail","title","thumbnailphoto");
$result = ldap_search($ldap_connection,$base_dn,$filter,$attr);
$rescount = ldap_count_entries($ldap_connection,$result);
$data = ldap_get_entries($ldap_connection,$result);
if ($data["count"] > 0)
{
for ($i=0; $i<$data["count"]; $i++)
{
echo "<p> sn: " . $data[$i]["sn"][0]."<br/>";
echo "givenname: ". $data[$i]["givenname"][0] ."<br/>" ;
echo "employeeID: " . $data[$i]["employeeid"][0]."<br/>";
echo "distinguishedName: " . $data[$i]["distinguishedname"][0]."<br/>";
echo "displayName: " . $data[$i]["displayname"][0]."<br/>";
echo "sAMAccountName: " . $data[$i]["samaccountname"][0]."<br/>";
echo "department: ". $data[$i]["department"][0]."<br/>";
echo "manager: " .$data[$i]["manager"][0]."<br/>";
echo "mail: ". $data[$i]["mail"][0]."<br/>";
echo "title: " .$data[$i]["title"][0]."<br/>";
//echo "photo: " .$data[$i]["thumbnailphoto"][0]."<br/>";
// echo "<br/><br/>";
}
}
else
{
echo "<p>No results found!</p>";
}
?>
&#13;
浏览器控制台显示上面的php返回:
<p> sn: xxxxxx<br/>givenname: xxxxx<br/>
employeeID: 0050<br/
>distinguishedName: CN=xxxx xxxxx,OU=Employees,OU=Accounts,OU=India,DC=asia,DC=xxxxxxx,DC=com<br/>
displayName: Mark Hewettk<br/>sAMAccountName: xxxxxxx<br/>
department: xxxxx<br/>manager: CN=xxxxxx xxxxxxx,OU=Employees,OU=Accounts,OU=India,DC=asia,DC=xxxx,DC=com
<br/>
mail: mhewettk@abc.com<br/>
title: xyz<br/>
&#13;
我只想从上面的数据中获取一些属性,例如mail,displayname等,并在我的HTML中显示:
<h2 class="profile__name" id="emailOfUser">Email : </h2>
&#13;
现在问题是我在这里使用的jquery:
$('.leaderboard li').on('click', function () {
$.ajax({
url: "../popupData/activedirectory.php", // your script above a little adjusted
type: "POST",
data: {id:$(this).find('.parent-div').data('name')},
success: function(data){
console.log(data);
$('#popup').fadeIn();
$('#emailOfUser').html(data); //this line displays all data whereas I want to select only email,displayname from the above console data
//whatever you want to fetch ......
// etc ..
},
error: function(){
alert('failed, possible script does not exist');
}
});
});
&#13;
问题是:
$('#emailOfUser').html(data);
&#13;
此行显示所有数据,而我只想从上述控制台数据中选择电子邮件,displayname
请帮助我如何从上述浏览器控制台数据中仅选择所需的属性数据。
我之前试过将它转换为JSON。但没有运气。How to return an array from an AJAX call?
答案 0 :(得分:0)
您可以将数据作为简单的JSON-Object传递,而不是将数据作为HTML字符串传递给客户端:
header('Content-Type: application/json');
echo json_encode($data);
通过这种方式,您可以在客户端更轻松地处理数据,因为它已经采用JavaScript理解的格式。
像...一样的东西。
$.ajax({
url: "../popupData/activedirectory.php"
...
}).then(function(response) {
// now you have access to the data as a plain JS-Object
$('#emailOfUser').html(response.email + " - " + response.displayname);
});
......应该这样做。