简体   繁体   English

Bootstrap 3 Typeahead Ajax

[英]Bootstrap 3 Typeahead Ajax

I'm using https://github.com/bassjobsen/Bootstrap-3-Typeahead 我正在使用https://github.com/bassjobsen/Bootstrap-3-Typeahead

My code: 我的代码:

<input type="text" class="typeahead" autocomplete="off">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
$(document).ready(function() {
    $('.typeahead').typeahead({
        autoSelect: true,
        minLength: 2,
        delay: 400,
        source: function (query, process) {
            $.ajax({
                url: '/search/searchCities',
                data: {q: query},
                dataType: 'json'
            })
                .done(function(response) {
                    console.log(response.data);
                    return process(response.data);
                });
        }
    });
});
</script>

My backend code returns an json object like 我的后端代码返回一个json对象,例如

{status: "OK", data: ["Moscow", "New York", "Odessa"]}

by php code 通过PHP代码

<?= echo json_encode(["status" => "OK", "data" => ["Moscow", "New York", "Odessa"]]) ?>

After I type 2 or more letters in the input there's an error in the browser console like this 在输入中输入2个或更多字母后,这样的浏览器控制台中就会出现错误

GET http://localhost/search/searchCities?q=mo 200 OK
TypeError: a is undefined
...rep:function(a,b,c){for(var d,e=[],f=0,g=a.length,h=!c;g>f;f++)d=!b(a[f],f),d!==...

jquery.min.js (row 2, col 3476)
["Moscow"]

I see the dropdown with "Moscow" option, I can choose it and everything would be fine if i had not got the error in console. 我看到带有“ Moscow”选项的下拉菜单,我可以选择它,并且如果控制台中没有错误,一切都会好起来的。 Please, show me my mistake. 请告诉我我的错误。

I tried to use jquery.js (not the min-version). 我尝试使用jquery.js(不是min-version)。 The error was 错误是

TypeError: elems is undefined
length = elems.length,

jquery.js (row 459, col 4)

I guess that something wrong with data type but i can't find it by myself. 我猜数据类型有问题,但我自己找不到。

事实证明,如果我使用bootstrap3-typeahead.js,bootstrap3-typeahead.min.js中有错误-浏览器控制台中没有错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM