0

A lot of jQuery plugins have AJAX support, for example the autocomplete jQuery UI plugin or the form validation plugin.

Most of the documentation about the AJAX support of the plugin is shown using PHP:

Autocomplete:

$(function() {
    $( "#birds" ).autocomplete({
        source: "search.php",
        minLength: 2,
        select: function( event, ui ) {
            log( ui.item ?
                "Selected: " + ui.item.value + " aka " + ui.item.id :
                "Nothing selected, input was " + this.value );
        }
    });
});

Validation plugin:

$("#myform").validate({
  rules: {
    email: {
      required: true,
      email: true,
      remote: "check-email.php"
    }
  }
});

My question is know how can this be done using the Flask Framework? And what type of object do I have to return?

Thanks in advance!

1 Answer 1

2

Configure the plugin to use one of your app's URLs, e.g. source: '/search', and define the view with this route:

@app.route('/search')
def search():
    # If it's a GET request, the data will be provided as request.args.  In case
    # of POST or PUT you'll have to use request.data or request.json (depends on
    # how the plugin is sending the data).
    query = request.args.get('query')
    # Perform the search here.
    results = ...
    # What to return here depends on what the plugin expects, consult the docs
    # to figure this out.  Most likely it'll be some JSON encoded data structure.
    return jsonify(results=results)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.