繁体   English   中英

使用ajax填充的JQuery自动完成缓存

[英]JQuery Autocomplete caching with ajax not populating

我试图通过ajax调用添加缓存到下面的代码。 在我第一次自动完成工作时添加缓存后,第二次显示空白时,代码可以正常缓存。 我在这做错了什么?

我的代码

$(document).ready(function () {
    $("#MainContent_txtSurname").autocomplete({

        source: function (request, response) {
            var term = request.term;
            if (term in cache) {
                response(cache[term]);
                return;
            }
            $.ajax({
                crossDomain: true,
                type: 'POST',
                url: "http://localhost:1448/GetSurnames",

                dataType: 'json',
                data: { "Name": request.term, "CID": CID },
                processdata: true,
                success: function (result) {
                    var Surnames = JSON.parse(result.data);

                    cache[term] = Surnames;
                    response($.map(Surnames, function (item) {

                        return {
                            label: item.homename,
                            value: item.homename
                        }
                    }));
                },
                error: function (a, b, c) {
                    debugger;
                }
            });

        },
        minLength: 2
    });
});

返回的数据是:

{"data":"[{\"id\":3,\"homename\":\"D\\u0027Costa\"}]"}

尝试缓存自动完成插件的正确格式数据。 关于你的Ajax成功:

success: function (result) {
    var Surnames = JSON.parse(result.data);

    cache[term] = $.map(Surnames, function (item) {

        return {
            label: item.homename,
            value: item.homename
        }
    });
    response(cache[term]);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM