尝试使用JQueryUI实现对 Windows 用户帐户的简单搜索。
要求用户在控件中输入名字或姓氏,该HTML
<input>
控件应返回该搜索项的全名与(用户名)的所有可能匹配项。虽然服务器返回结果如下:
问题:该<input>
框显示搜索词,并显示没有选项的“白色”下拉菜单。
jQuery代码:
$(document).ready(function () {
$("#nameSearch").autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Search.aspx/GetUserDetails",
data: "{'username':'" + request.term + "'}",
dataType: "json",
async: true,
success: function (data) {
response($.map(data, function (item) {
return {
value: item.username
}
}));
},
error: function (xhr, textStatus, errorThrown) {
var errorMessage = "Ajax error: " + this.url + " textStatus: " + textStatus + " errorThrown: " + errorThrown + " xhr.statusText: " + xhr.statusText + " xhr.status: " + xhr.status;
alert(errorMessage);
if (xhr.status != "0" || errorThrown != "abort") {
alert(xhr.responseText);
}
}
});
}
});
});
背后的代码
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static Person[] GetUserDetails(string username)
{
List<Person> allUsers = new List<Person>();
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "abcd",
"dc=abcdH,dc=com");
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.GivenName = username;
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);
foreach (var found in srch.FindAll())
{
Person user = new Person();
user.userDetails = found.DisplayName + " (" + found.SamAccountName + ")";
allUsers.Add(user);
}
qbeUser = null;
qbeUser = new UserPrincipal(ctx);
qbeUser.Surname = username;
PrincipalSearcher srch1 = new PrincipalSearcher(qbeUser);
foreach (var found in srch1.FindAll())
{
Person user = new Person();
user.userDetails = found.DisplayName + " (" + found.SamAccountName + ")";
allUsers.Add(user);
}
qbeUser = null;
qbeUser = new UserPrincipal(ctx);
qbeUser.SamAccountName = username;
PrincipalSearcher srch2 = new PrincipalSearcher(qbeUser);
foreach (var found in srch2.FindAll())
{
Person user = new Person();
user.userDetails = found.DisplayName + " (" + found.SamAccountName + ")";
allUsers.Add(user);
}
//allUsers.Sort();
return allUsers.ToArray();
}
public class Person
{
public string userDetails { get; set; }
}
我一定是做错了什么,我不能马上发现。从 SO 答案中尝试了许多不同的片段,但不适合我的问题。