【问题标题】:How to format response to jQuery AutoComplete如何格式化对 jQuery AutoComplete 的响应
【发布时间】:2013-06-03 11:41:05
【问题描述】:

我正在尝试使用 jQuery AutoComplete,但我得到的只是一个包含所有结果的单个项目。

我有这个 ASP.Net WebMethod:

[WebMethod]
public static string FetchCompletionList(string term)
{
    var json = JsonConvert.SerializeObject(CustomerProvider.FetchKeys(term, 8));
    return json;
}

被此脚本调用:

$("[id$='txtLKey']").autocomplete({
            minlength: 2,
            source: function(request, response) {

                $.ajax({

                    type: "POST",
                    url: "/Views/Crm/Json/Json.aspx/FetchCompletionList",
                    data: '{term: "' + $("[id$='txtLKey']") .val() + '", count: "8"}',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        response(data);
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                       alert(jqXHR.responseText);
                       alert(textStatus);
                       alert(errorThrown.toString());
                    }
                });

            }
        });

结果是这样的:

当我真正想要的是用户可以选择的选项列表时,即每个 NZ 应该是列表中的一个项目。

【问题讨论】:

  • 可以分享一下服务器返回的值吗?在浏览器中使用 firebug/developer 工具查看响应
  • 看那是我不明白的。没有标签/值,它只是一个字符串数组而不是字典。文档jqueryui.com/autocomplete/#default 将字符串数组作为源输入到小部件中。为什么远程返回的数据需要有字典形式的额外“无意义”绒毛?据我所见,文档没有发现任何需要为远程返回的数据使用不同的格式。谢谢
  • 对不起,我的陈述是错误的,自动完成在内部标准化了传递给response的输入。那么原因可能是data 可能不是一个数组,它可能是一个字符串。你能用console.log(data)看看打印的是什么
  • {"d":"[\"NZ0008\",\"NZ0015\",\"NZ0017\",\"NZ0018\",\"NZ0026\",\"NZ0027\" ,\"NZ0031\",\"NZ0035\"]"} 来自萤火虫
  • 这里有两个问题,1.你需要通过data.d响应response(data.d);你的值字符串是无效的应该是{"d":["NZ0008","NZ0015","NZ0017","NZ0018","NZ0026","NZ0027","NZ003??1","NZ0035"]}

标签: jquery-ui autocomplete


【解决方案1】:

这里有两个问题,

  1. 您需要将data.d 传递给response,例如response(data.d)
  2. 您的值字符串无效,应为{"d":["NZ0008","NZ0015","NZ0017","NZ0018","NZ0026","NZ0027","NZ003??1","NZ0035"‌​]}

【讨论】:

  • 好的,如果我通过 data.d 我什么也得不到自动完成。即没有清单。我认为萤火虫输出中的逃逸是由萤火虫放在那里的。它们不会出现在 console.log 中。
  • 测试用例将对数据进行硬编码,看看发生了什么,比如在data = {"d":["NZ0008","NZ0015","NZ0017","NZ0018","NZ0026","NZ0027","NZ003??1","NZ0035"‌​]}之前添加response(data);
  • AFAIK console.log 不会添加任何转义字符,您也可以使用 firfox 中的 net 选项卡查看服务器的实际响应
  • 我在数组周围加了引号....而且我没有 data.d,只有数据;(非常感谢。
猜你喜欢
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-03
  • 2022-01-22
  • 2020-06-04
  • 2012-06-02
相关资源
最近更新 更多