Typeahead.js没有发送ajax请求

时间:2014-09-25 10:50:40

标签: ruby-on-rails ajax typeahead.js

我想将typeahead.js与json文件一起使用。但是当我输入预先输入时,不会发送ajax请求。

的javascript:

var users = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  limit: 10,
  prefetch: {
    url: '/users.json',
    filter: function(list) {
      return $.map(list, function(user) { return { name: user }; });
    }
  }
});

users.initialize();

$('#prefetch .typeahead').typeahead(null, {
  name: 'users',
  displayKey: 'name',
  // `ttAdapter` wraps the suggestion engine in an adapter that
  // is compatible with the typeahead jQuery plugin
  source: users.ttAdapter()
});

rails controller:

def index
    @users = User.all
    @search = User.select :name
    render json: @search
end

1 个答案:

答案 0 :(得分:0)

这是使用的正确代码: 在 javascript

var users = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  limit: 10,
  remote: {
    url: '/users.json',
    filter: function(list) {
      return $.map(list, function(user) { return { name: user }; });
    }
  }
});