如何格式化对jQuery AutoComplete的响应

时间:2013-06-03 02:45:15

标签: jquery-ui autocomplete

我试图使用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());
                    }
                });

            }
        });

结果如下:

enter image description here

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

1 个答案:

答案 0 :(得分:0)

这里有两个问题,

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