如何避免从Jquery Autocomplete进行多次ajax调用?使用缓存 [英] How to avoid multiple ajax call from the Jquery Autocomplete ? Using Cache

查看:317
本文介绍了如何避免从Jquery Autocomplete进行多次ajax调用?使用缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将jquery automcompleteAjax调用一起使用,但是我想要的是如果在第一次调用时由ajax提取的json数据中存在part,那么我想按原样返回该数据没有给ajax打电话,我已经尝试过如下

I am using jquery automcomplete with the Ajax call but what i want is if part is present in the json data fetched by the ajax on first call then i want to return that data as it is without giving the ajax call i have tried it as below

  function SearchText() {
        var cache = {};
        $("#txtItem").autocomplete({
            source: function (request, response) {
                var term = request.term;
                $.each(cache, function (index, value) {
                    $.each(value, function (index, value) {
                        if (value.indexOf(term) >= 0) {
                            response(cache[term]);
                            return;
                        }
                    });
                });
                cache = {};
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "JobTagPricing.aspx/GetAutoCompleteData",
                    data: "{'item':'" + document.getElementById('txtItem').value + "'}",
                    dataType: "json",
                    success: function (data) {
                        cache[term] = data.d;
                        response(data.d);
                    },
                    error: function (result) {
                        alert("Error");
                    }
                });
            },
            minLength: 3
        });
    }

,但是即使它在数组中找到匹配的term,它也会生成ajax调用. 我被困在这里3个小时了,任何帮助都会很棒.我已经尝试过maxCacheLength,但它也无法正常工作.

but even if it finds the matching term in the array then also it generates the ajax call. I m stuck here for 3 hrs now any help would be great. I have tried maxCacheLength but it is also not working.

推荐答案

尝试一下,不久前,我遇到了同样的问题,想出了一个可能对您有用的方法

Try this out, sometime ago i faced same problem and came up with this it might work for you

   function SearchText() {
        var cache = {};
        var oldterm;
        $("#txtItem").autocomplete({
            source: function (request, response) {
                if (request.term.indexOf(oldterm) >= 0) {
                    if (typeof (oldterm) != 'undefined') {
                        var data = jQuery.grep(cache[oldterm],
                    function (ele) {
                        return (ele.indexOf(request.term) >= 0);
                    });
                        response($.map(data, function (item) {
                            return { value: item }
                        }))
                        return;
                    }
                } else {
                    cache = {};
                    $.ajax({
                        url: "JobTagPricing.aspx/GetAutoCompleteData",
                        data: "{ 'item': '" + request.term + "' }",
                        dataType: "json",
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        dataFilter: function (data) { return data; },
                        success: function (data) {
                            oldterm = request.term;
                            cache[request.term] = data.d;
                            response($.map(data.d, function (item) {
                                return {
                                    value: item
                                }
                            }))
                        },
                        error: function (result) {
                            alert("Error");
                        }
                    });
                }
            },
            minLength: 3,
            select: function (event, ui) {
                if (ui.item) {
                    formatAutoComplete(ui.item);
                }
            }
        });
    }

这篇关于如何避免从Jquery Autocomplete进行多次ajax调用?使用缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆