0

很多 jQuery 插件都支持 AJAX,例如自动完成 jQuery UI 插件或表单验证插件

大多数关于插件的 AJAX 支持的文档都是使用 PHP 显示的:

自动完成:

$(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 );
        }
    });
});

验证插件:

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

我的问题是知道如何使用 Flask 框架来做到这一点?我必须返回什么类型的对象?

提前致谢!

4

1 回答 1

2

将插件配置为使用您的应用程序的 URL 之一,例如source: '/search',并使用此路由定义视图:

@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)
于 2012-12-26T10:13:15.157 回答