This question has been asked before, and I think I've done what I've seen there, but I don't really know what I'm doing wrong. I don't know a lot about jQuery, but I'll do my best to explain what I'm trying to do.
I want to autocomplete based on a query from a database, so I have this in my template:
<script type="text/javascript">
$(function() {
$( "#function_name" ).autocomplete({
source: '{{url_for("autocomplete")}}',
minLength: 2,
});
});
</script>
<form id="function_search_form" method="post" action="">
{{form.function_name}}
</form>
The form is generated by this Flask form class:
class SearchForm(Form):
function_name = TextField('function_name', validators = [Required()])
And here is the autocomplete function:
@app.route('/autocomplete')
def autocomplete():
results = []
search = request.args.get('term')
results.append(db.session.query(Table.Name).filter(Table.Name.like('%' + search + '%')).all())
return dumps(results)
So the jQuery should go to the autocomplete function and get some JSON back to autocomplete with. At least I think that's what's going on. What am I doing wrong here?