按照自动完成的官方示例,我想出了这个。
$("#search").autocomplete({
source: function (request, response) {
$.ajax({
url: "/search",
dataType: "jsonp",
data: {
featureClass: "P",
style: "full",
maxRows: 12,
name_startsWith: request.term
},
success: function (data) {
response($.map(data.username, function (item) {
return {
label: item.name,
value: item.name
};
}));
},
error: function (data) {
}
});
},
minLength: 2,
select: function (event, ui) {
},
});
但是我从来没有打过,success: function (data) { .. }
但总是打error: function (data) {
。
Firebug 显示错误:"parsererror", SyntaxError: invalid label
/search
url 在服务器端生成:
def search():
data = None
if 'name_startsWith' in request.args:
q = request.args['name_startsWith']
data = User.query(ndb.AND(User.firstname >= q, User.firstname <= q + u'\ufffd')).fetch(12)
js = []
for user in data:
js.append({'name' : user.fullname()})
return jsonify(username=js)
请问有什么建议吗?