【发布时间】:2013-12-31 17:29:48
【问题描述】:
以下代码运行良好。我想处理错误情况,例如如果服务器的结果是空对象或搜索时出现一些数据库错误,我应该如何捕捉错误并显示一些消息?
var nameIdMap = {};
$('#selectAgent').typeahead({
source: function (query, process) {
var that = this;
return $.ajax({
dataType: "json",
url: '/eBus/EbusinessAgentServlet',
data: 'q=' + query +"&task=tab1&AgentId=1234",
type: 'POST',
beforeSend: function() {
//that.$element is a variable that stores the element the plugin was called on
that.$element.addClass('loading');
},
complete: function() {
that.$element.removeClass('loading');
},
success: function (json) {
console.log(json)
process(getOptionsFromJson(json));
}
});
},
minLength: 2,
updater: function (item) {
console.log('selected id'+nameIdMap[item]);
return item;
}
});
});
function getOptionsFromJson(json) {
$.each(json, function (i, v) {
nameIdMap[v.fname + " "+ v.lname + " " + v.agentID] = v.agentID;
});
return $.map(json, function (n, i) {
return n.fname +" " + n.lname+" " + n.agentID;
});
}
【问题讨论】:
标签: jquery bootstrap-typeahead