我试图在asp.net web forms app中实现typeahead.js。
我有一个通用的处理程序,它返回字符串列表,在我的例子中,是学校名称。
这是处理程序的代码:
public void ProcessRequest(HttpContext context)
{
List<string> strSchools = new List<string>();
string prefixText = context.Request.QueryString["q"];
var schools =
DataLayer.GetSchools()
.Select(s => s).OrderBy(s => s.Name);
foreach (SchoolItem school in schools)
{
if (prefixText != string.Empty)
{
prefixText = prefixText.ToLower();
if (school.Name.ToLower().Contains(prefixText))
{
strSchools.Add(school.Name);
}
}
else
{
strSchools.Add(school.Name);
}
}
JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
context.Response.Write(jsSerializer.Serialize(strSchools));
}
在js代码中我有这个:
function pageLoad(sender, args) {
var schoolList = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('names'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
limit: 10,
remote: 'SchoolListHandler.ashx'
});
// kicks off the loading/processing of `local` and `prefetch`
schoolList.initialize();
// debugger;
// passing in `null` for the `options` arguments will result in the default
// options being used
$('#txtSchool').typeahead(null, {
name: 'name',
displayKey: 'Name',
// `ttAdapter` wraps the suggestion engine in an adapter that
// is compatible with the typeahead jQuery plugin
source: schoolList.ttAdapter()
});
}
由于某些原因,我说我无法到达处理程序并获得结果。
你知道有什么不对吗?