【问题标题】:Typeahead/Bloodhound - Using Jquery Ajax for remote causes only a single server side requestTypeahead/Bloodhound - 使用 Jquery Ajax 进行远程只会导致单个服务器端请求
【发布时间】:2014-05-14 13:23:02
【问题描述】:

我需要在 Bloodhound 的远程属性中使用 jquery ajax 设置,因为我有一个只接受 POST 请求的服务器端页面。一切正常,但只有一次。对预先输入框中文本的任何后续更改都会调用过滤器函数,但不会触发新的服务器端请求以获取新数据。它只是过滤在第一个请求中获得的数据。当用户删除文本并输入其他内容时,我需要它发出新请求。

我是 typeahead 的新手,我花了太多时间试图弄清楚这一点。这是我的代码。

var users = new Bloodhound({
        datumTokenizer: function (d) {
            return Bloodhound.tokenizers.whitespace(d.value);
        },
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        remote:   {
            url: 'fake.jsp',
            filter: function (users) {
                return $.map(users, function (user) {
                    return {
                        value: user.USER_ID,
                        name: user.DISPLAYNAME
                    };
                });
            },
            ajax: {
                type: 'POST',
                data: {
                    param: function(){
                        return $('#userid').val();
                    }
                },
                context: this
            }
        }
    });
    users.initialize(true);

    $('#userid').typeahead({
          minLength: 3,
          highlight: true
        }, {
          name: 'userslist',
          displayKey: 'name',
          source: users.ttAdapter()
    });

【问题讨论】:

  • 我能够解决这个问题。需要使用 Bloodhound 的 clearRemoteCache 方法。
  • 你具体在哪里使用 clearRemoteCache()?

标签: typeahead bloodhound


【解决方案1】:

我有相同的解决方案,发现 jQuery 的 cache: false; 选项在这种情况下无论出于何种原因都不起作用。这是我找到的解决方案:

   remote: {
        url: ...
        replace: function(url, query) {
            return url + "#" + query; // used to prevent the data from being cached. New requests aren't made without this (cache: false setting in ajax settings doesn't work)
        }
    }

【讨论】:

    【解决方案2】:

    试试这个:

       remote:   {
                   url: 'fake.jsp/?' + Math.random(),
       .
       .
       .
    

    这不是真正的解决方案,但至少每次刷新页面时都会从服务器获取结果。

    【讨论】:

      猜你喜欢
      • 2014-03-16
      • 2013-09-18
      • 1970-01-01
      • 2011-01-16
      • 2016-02-27
      • 2011-07-24
      • 2019-06-01
      • 2018-01-31
      • 1970-01-01
      相关资源
      最近更新 更多