twitter-bootstrap Bootstrap 3 Typeahead Ajax
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35894682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bootstrap 3 Typeahead Ajax
提问by Andrew P.
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. Please, show me my mistake.
我看到带有“莫斯科”选项的下拉菜单,我可以选择它,如果我没有在控制台中收到错误,一切都会好起来的。请告诉我我的错误。
I tried to use jquery.js (not the min-version). The error was
我尝试使用 jquery.js(不是最小版本)。错误是
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.
我猜数据类型有问题,但我自己找不到。
回答by Andrew P.
It turned out that there is a mistake in bootstrap3-typeahead.min.js If I use bootstrap3-typeahead.js - no errors in the browser console.
结果是 bootstrap3-typeahead.min.js 有错误如果我使用 bootstrap3-typeahead.js - 浏览器控制台没有错误。

