gpt4 book ai didi

jquery - 通过 Bloodhound 远程建议提前输入

转载 作者:行者123 更新时间:2023-12-01 02:53:36 24 4
gpt4 key购买 nike

这是我的代码:

tagsProcessor(){
const suggestions = [{value: 'string1'}, {value: 'string2'}, {value: 'string3'}, {value: 'string4'}, {value: 'string5'}]
var bloodhoundSuggestions = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
sufficient: 3,
local: suggestions,
remote: {
url: 'http://localhost:3001/api/suggestions',
}
});

const $tagsInput = $('#tagsInput')
$tagsInput.typeahead({
hint: true,
highlight: true,
minLength: 1
},
{
name: 'suggestions',
displayKey: 'value',
source: bloodhoundSuggestions
});

}
}

它可以很好地处理本地建议,但由于某种原因它不适用于远程。

url http://localhost:3001/api/suggestions 返回一组对象,与 suggestions 常量类似。

为什么预先输入中没有显示来自远程的建议?

此函数收到远程建议后,我立即在控制台中收到此错误:

Uncaught TypeError: Cannot read property 'length' of undefined jQuery.extend.each @ libs.js:358 _.each @ libs.js:17928 processRemote @ libs.js:18763 onResponse @ libs.js:18515 done @ libs.js:18254 jQuery.Callbacks.fire @ libs.js:3148 jQuery.Callbacks.self.fireWith @ libs.js:3260 done @ libs.js:9314 jQuery.ajaxTransport.options.send.callback @ libs.js:9718

更新 1我的远程数据是由nodeJS服务器API返回的:

router.route('/suggestions')
.get(function(req, res) {
res.json([{value: 'data10'}, {value: 'data30'}, {value: 'data20'}, {value: 'data40'}, {value: 'data50'}])
});

更新2我确信我从服务器收到了正确的答案,因为我在 console.log 中看到它:

var bloodhoundSuggestions = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: suggestions,
remote: {
url: 'http://localhost:3001/api/suggestions',
transform: function(argument) {
console.log('argument', argument)
return argument
}
}
});

最佳答案

使用远程数据源的代码示例如下:

https://jsfiddle.net/ka0v6bg7/

尝试搜索以“data”或“string”开头的字符串(注意,查询“data”将花费更长的时间,因为它是远程数据源!)

我唯一改变的是远程数据源。

因此需要检查的是:

1) 您正确引用了 typeahead。尝试从这里引用它:

https://cdnjs.cloudflare.com/ajax/libs/typeahead.js/0.11.1/typeahead.bundle.min.js

作为测试。

2) Jquery错误提示可能没有正确引用。再次,作为测试尝试从这里引用 JQuery:

https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js

关于jquery - 通过 Bloodhound 远程建议提前输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33082444/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com