【发布时间】:2015-10-12 13:29:49
【问题描述】:
这是我的代码:
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 常量。
为什么在预先输入的输入中没有显示来自远程的建议?
我在此函数收到远程建议后立即在控制台中收到此错误:
未捕获的类型错误:无法读取未定义的属性“长度” 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 完成@ 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
}
}
});
【问题讨论】:
-
你的远程文件是一个数组?还是 Json?
-
你的 Json 有多少输入?因为遥控器有限
-
您能否使用“网络”面板或数据包嗅探器来确保您的远程服务器与本地服务器的响应方式完全相同?看起来它没有正确解析响应。
-
我的回答对你有帮助吗?
-
OP,下面的有用答案有帮助吗?请以某种方式回复它(例如接受它)。
标签: jquery local remote-server typeahead.js bloodhound