比方說,我鍵入例如「j」,我應該看到自動完成,如約翰在輸入標籤下面有更多的建議,但我不知道。在我的控制檯我得到["John", "Jane"]
,沒有錯誤。typeahead自動完成的建議與AJAX不起作用
的test.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8"/>
</head>
<body>
<div id="aa">
<input class="typeahead" type="text" placeholder="names">
</div>
<script src="../static/jquery-1.11.3.min.js"></script>
<script src="../static/typeahead.bundle.js"></script>
<script>
$('#aa .typeahead').typeahead(null, {
source: function (query, process) {
$.ajax({
url: '/test/',
type: 'GET',
contentType: "application/json; charset=utf-8",
data: {'query': query},
success: function(data) {
console.log(data.options);
process(data.options);
}
});
}
});
</script>
</body>
</html>
app.py
from flask import Flask, render_template, url_for, jsonify, request
app = Flask(__name__)
@app.route('/')
def index():
return render_template('test.html')
@app.route('/test/')
def test():
print request.args.get('query')
return jsonify(options=["John","Jane"])
if __name__ == '__main__':
app.run(debug = True)
顯然,您期待'process'做些什麼......它沒有這樣做......您希望'process'做什麼?是lookahead.js api的一部分嗎? –