我想使用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');
}
});
答案 0 :(得分:2)
根据typeahead API,服务器响应应标记为Async,并且应使用该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);
}
});
}
});
因为此问题有公开赏金,所以我不能将其标记为重复,但您可能会在以下问题中找到更多详细信息,