【问题标题】:JQuery Autocomplete with source pointing to .aspx带有指向 .aspx 的源的 JQuery 自动完成
【发布时间】:2015-03-06 14:21:27
【问题描述】:

无论我如何返回数据,作为 List 或 JSON 字符串,jQuery Autocomplete 插件都不会显示下拉列表中的值。

Javascript:

$("#myText").autocomplete({
    source: function (request, response) {
        $.ajax({
            type: "POST",
            url: "Default.aspx/GetList",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: "{'term':'" + request.term + "'}",
            success: function (data) {
                response(data);
            },
            error: function (xhr, error) {
                console.debug(xhr); console.debug(error);
            }
        });
    },
    minLength: 0,
    select: function (event, ui) {
        var result = ui.item.id;
    }
});

服务器端 (.aspx):

[ScriptMethod(ResponseFormat = ResponseFormat.Json), WebMethod]
public static string GetList(string term)
{
    List<string> list = new List<string>();
    list.Add("apple");
    list.Add("apricot");
    list.Add("apple cider");

    string json = "[" + string.Join(",",
    list.Select(i =>
        "{ 'id': '" + i + "'" + "}"
    )) + "]";

    return json;
}

我附上了两张图片,第一张是在浏览器中的显示方式,第二张是javascript中的断点。

我做错了什么?

【问题讨论】:

    标签: jquery jquery-autocomplete


    【解决方案1】:

    使用了 JavaScriptSerializer,它成功了。

    [ScriptMethod(ResponseFormat = ResponseFormat.Json), WebMethod]
    public static string GetList(string term)
    {
        List<string> list = new List<string>();
        list.Add("apple");
        list.Add("apricot");
        list.Add("apple cider");
    
        JavaScriptSerializer serialize = new JavaScriptSerializer();
        string result = serialize.Serialize(list);
        return result;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多