微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Jquery Typeahead源码传递ajax响应数据

如何解决Jquery Typeahead源码传递ajax响应数据

我正在实现 jquery 的 typeahead 并且在响应中传递 ajax 响应时遇到问题。

我尝试了很多,但都失败了。

这是代码

// search form typeahead
    var findData = function () {
        return function findMatches(q,cb) {
             $.get("http://127.0.0.1:8000/api/v1/fun-search/",(data,status) => {
                 console.log(data.dataArray)
                 cb(data.dataArray);
             });
        };
    };


    $('#the-basics .typeahead').typeahead({
            hint: true,highlight: true,minLength: 1
        },{
            name: 'items',source: findData()
        });

但是我在没有像下面这样的ajax调用的情况下工作得很好:

// search form typeahead
    var findData = function () {
        var items = ['Armchair','Sofa'];
        
        return function findMatches(q,cb) {
         
            
            cb(items);
        };
    };


    $('#the-basics .typeahead').typeahead({
            hint: true,source: findData()
        });

在这上面浪费了 5 天时间并没有解决。谁能告诉我的 ajax 数据不起作用的确切原因是什么?我尝试了很多不同的方法,但都失败了。

API 运行良好,响应数组现在与虚拟项目数组相同,但它不起作用。

谁能告诉我那里发生了什么确切的问题?

我正在使用以下 tyeahead 版本。

/*!
 * typeahead.js 0.11.1
 * https://github.com/twitter/typeahead.js
 * copyright 2013-2015 Twitter,Inc. and other contributors; Licensed MIT
 */

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。