嘗試使用JQueryUI實現對Windows用戶帳戶的簡單搜索。jQueryUI自動完成不會將結果呈現給輸入框
要求用戶輸入名字或姓氏的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; }
}
我必須在這裏做得不對,我不能通俗易懂發現。嘗試了很多不同的答案,但不符合我的問題。
對不起,答案沒有幫助。 – shaz