2

大家好,我正在使用 bootstrap-typeahead.js 来获取我的烧瓶项目中所有用户列表的 json 数据。

根据 Bootstrap Type ahead 的基本行为,我可以在 type ahead 调用函数下查看指定的 json 数据,但不知道如何获取 json 数据。

此代码段将采用 JSON 城市列表,然后使用 typeahead 很好地显示

<p>Type Query: <input type="text" class="input-medium search-query" id="cities" data-provide="typeahead" data-items="4" ></input></p>

<script type="text/javascript"></script>
$('#cities').typeahead({
    source: [
        { id: 1, name: 'Toronto' },
        { id: 2, name: 'Montreal' },
        { id: 3, name: 'New York' },
        { id: 4, name: 'Buffalo' },
        { id: 5, name: 'Boston' },
        { id: 6, name: 'Columbus' },
        { id: 7, name: 'Dallas' },
        { id: 8, name: 'Vancouver' },
        { id: 9, name: 'Seattle' },
        { id: 10, name: 'Los Angeles' }
    ]

    });

但是当我想从远程源或从文件中获取数据时,根据这个twitter-bootstrap-typeahead应该是这样的

$('#cities').typeahead({
    ajax: '/sample/list'
});  

但这不符合我的要求。

我有这个视图来获取 Python Flask 中的所有用户

@app.route('/users')
@login_required
def get_all_users():
  all_users =  model.User.query()
  first = {}; users = []
  for user in all_users:
    user_key = user.key
    first['uname'] = user.name
    first['uuid'] = user_key.integer_id()
    first['about_me'] = user.about_me
    first['email'] = user.email
    users.append(first)
    first = {}
  return jsonify(users=users)

它会抛出类似这样的数据

{
  "users": [
    {
      "about_me": null, 
      "email": "chitrankdixit@gmail.com", 
      "uname": "Chitrank", 
      "uuid": 5066549580791808
    }, 
    {
      "about_me": null, 
      "email": "test@example.com", 
      "uname": "Test", 
      "uuid": 5629499534213120
    }
  ]
}

现在我完全困惑为什么 typeahead 没有从我的烧瓶视图中获取 JSON 数据。请帮忙

4

0 回答 0