Flask + jquery autocomplete

后端 未结 2 735
一生所求
一生所求 2021-01-31 22:17

Yet another question about flask and jquery autocomplete. I have been reading and trying the following answered questions without any success:

Flask AJAX Autocomplete

相关标签:
2条回答
  • 2021-01-31 22:59

    Try this

    from flask import jsonify
    
    NAMES=["abc","abcd","abcde","abcdef"]
    
    @app.route('/autocomplete',methods=['GET'])
    def autocomplete():
        search = request.args.get('term')
    
        app.logger.debug(search)
        return jsonify(json_list=NAMES) 
    

    In Jquery:

    <script>
    $(function() {
        $.ajax({
            url: '{{ url_for("autocomplete") }}'
            }).done(function (data) {
                $('#autocomplete').autocomplete({
                    source: data.json_list,
                    minLength: 2
                });
            });
        });
    </script>
    
    0 讨论(0)
  • 2021-01-31 23:02

    It doesn't work because a missprint in a suggested piece of code:

    from flask import jsonify
    NAMES=["abc","abcd","abcde","abcdef"]
    @app.route('/autocomplete',methods=['GET'])
    def autocomplete():
        search = request.args.get('term')
    

    Change in search = request.args.get('term') 'term' to 'autocomplete'. So correct code should be as follow:

    from flask import jsonify
    NAMES=["abc","abcd","abcde","abcdef"]
    @app.route('/autocomplete',methods=['GET'])
    def autocomplete():
        search = request.args.get('autocomplete')
    
    0 讨论(0)
提交回复
热议问题