【问题标题】:typeahead.js not getting data from remote data source in asp.nettypeahead.js 没有从 asp.net 中的远程数据源获取数据
【发布时间】:2015-08-18 16:58:48
【问题描述】:

我正在尝试在 asp.net 网络表单应用程序中实现 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()
            });
  }

由于某种原因,正如我所说,我无法到达处理程序并取回结果。

你们觉得有什么不对吗?

【问题讨论】:

  • 你能提到你遇到的错误吗?

标签: javascript jquery asp.net typeahead.js typeahead


【解决方案1】:

有几件事跳出来 首先,检查你从 JS 的远程调用,

remote: {
        url: '/SchoolListHandler.ashx?query=%QUERY',
        wildcard: '%QUERY'
    }

接下来,检查您是否正在搜索名为“query”的输入参数

string prefixText = context.Request.QueryString["query"]; // not q

最后,请确保您正在设置退货的内容类型。将此添加到 .ashx 的末尾

Response.ContentType = "text/javascript";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-13
    • 2022-07-09
    • 2014-06-25
    • 2016-04-01
    • 2013-08-03
    • 1970-01-01
    • 2010-11-24
    • 1970-01-01
    相关资源
    最近更新 更多