我想使用 typeahead 远程检索邮政编码,并且必须是 post,而不是 get。该调用在控制台中返回以下 json:
{suggestions: "L-6956 | IM GRUND | KEEN-SUR-DOHEEM"}
{suggestions: "L-6956 | OP DER MOUCK | KEEN-SUR-DOHEEM"}
但是结果不会显示在输入字段下以便选择结果之一。这是我的代码:
$('#txtPostalCode').typeahead(
null,
{
name: 'txtPostalCode',
displayKey: 'suggestions',
minLength: 3,
source: function (query, syncResults) {
$.post('/hotbed/sggl/getpipedaddresses', {searchItem: query}, function (data) {
syncResults($.map(data, function (item) {
console.log(item.suggestions);
return item;
}));
}, 'json');
}
});
最佳答案
根据 typeahead API,服务器响应应标记为异步,并且应使用该 asyncCB 获取您的响应,
$('#txtPostalCode').typeahead({
null
},
{
name: 'txtPostalCode',
displayKey: 'suggestions',
minLength: 3,
async: true,
source: function (query, processSync, processAsync) {
processSync(['This suggestion appears immediately', 'This one too']);
return $.ajax({
url: "/hotbed/sggl/getpipedaddresses",
type: 'POST',
data: {searchItem: query},
dataType: 'json',
success: function (json) {
// in this example, json is simply an array of strings
return processAsync(json);
}
});
}
});
由于此问题有公开悬赏,我无法将其标记为重复,但您可能会在以下问题中找到更多详细信息,
关于javascript - typeahead js不显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48501988/