好的,这里是我想要做的,我想从活动目录中获取所有用户并将其放入列表中,所以我会从ajax调用Web服务,以获取所有用户并将其放入列表字符串中,后来我想在文本框中使用jquery自动完成,基于我之前获得的用户列表。如何正确调用ajax中的web服务?
这是我做的:
$(document).ready(function() {
// Load data then initialize plugin:
$.ajax({
url: '/SvcADUser.asmx/GetADUserList',
dataType: 'json'
}).done(function (source) {
var countriesArray = $.map(source, function (value) { return { value: value }; }),
countries = $.map(source, function (value) { return value; });
// Setup jQuery ajax mock:
$.mockjax({
url: '*',
responseTime: 200,
response: function (settings) {
var query = settings.data.query,
queryLowerCase = query.toLowerCase(),
suggestions = $.grep(countries, function (country) {
return country.toLowerCase().indexOf(queryLowerCase) !== -1;
}),
response = {
query: query,
suggestions: suggestions
};
this.responseText = JSON.stringify(response);
}
});
// Initialize autocomplete with local lookup:
$('#MainCT_dtvJobVac_PIC').autocomplete({
lookup: countriesArray,
onSelect: function (suggestion) {
$('#selection').html('You selected: ' + suggestion.value + ', ' + suggestion.data);
}
});
});
}());
}());
但这扔我一个错误,"NetworkError: 500 Internal Server Error - http://localhost:60525/SvcADUser.asmx/GetADUserList"
,如果我改变的URL SvcADUser.asmx,它不给一个错误,但让我没有结果。
我在这里做错了什么?顺便说一句,这里是我的web服务代码:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class SvcADUser : System.Web.Services.WebService
{
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[WebMethod]
public List<string> GetADUserList(string keyword)
{
List<string> alluser = new List<string>();
using (var context = new PrincipalContext(ContextType.Domain, "weekendinc.com"))
{
using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))
{
foreach (var result in searcher.FindAll())
{
DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
alluser.Add((string)de.Properties["samAccountName"].Value);
}
}
}
var filtereduser = alluser.Where(usr => usr.ToLower().StartsWith(keyword.ToLower()));
return filtereduser.ToList();
}
}
当您运行调试ASMX文件和手动输入的关键字是什么屏幕输出/错误? – 2013-05-14 07:03:19
nope,我的asmx文件没有返回错误,它工作正常,它返回我的用户列表。 – NomNomNom 2013-05-14 07:19:41
好的网络服务正在工作。尝试并将成功和错误放入你的ajax中。看看你是否可以捕获response.d并从中检索数据。我努力了一段时间才把自己弄对了,所以我最终写了一篇[博客文章](http://wimombelets.blogspot.be/2013/03/consuming-aspnet-web-services-with-ajax.html)关于它,所以我不会忘记。也许你会发现它有一些用处。 – 2013-05-14 07:25:03