如果数据在服务器中不可用,或者服务器出错,或者服务器数据为空,javascript会抛出异常obj is null。如何捕获此异常?
在stackoverflow中搜索没有给出任何结果。
代码如下:
var test1 = new Bloodhound({
datumTokenizer: function (d) {
return Bloodhound.tokenizers.whitespace(d.value);
},
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: "http://URL?query=",
replace: function(url, query) {
return url + "" + query;
}
}
});
test1.initialize();
$('#idOfAutoCompleteTextBox').typeahead({
hint: true,
highlight: true,
minLength: 1
},
{
name: 'test1',
displayKey: 'value',
source: test1.ttAdapter()
});发布于 2015-06-04 22:06:17
我不确定这是不是你的问题的解决方案,但目前我在处理空结果时遇到了相同的javascript异常(typeahead 0.11.1)。
当array为空时,在远程URL文件中回显非JSON格式的内容。
JavaScript
var termekek = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('megnev'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: _BASE_ + 'components/com_rexwebshop/include/keres.php?q=%QUERY',
wildcard: '%QUERY'
}
});
jQuery('.typeahead').typeahead(null, {
name: 'rs3ws-termekek',
display: 'megnev',
source: termekek,
templates: {
empty: [
'<div class="empty-message">',
'<? echo JTEXT::_('Nincs találat') ?>',
'</div>'
].join('\n'),
suggestion: Handlebars.compile('<div><strong>{{megnev}}</strong> – {{bruttoar}}</div>')
}
});PHP
// [collecting data]
if ( count($rows) == 0 )
echo "_NOT_FOUND_";
else
echo json_encode($rows, JSON_UNESCAPED_SLASHES);https://stackoverflow.com/questions/24434002
复制相似问题