2012-12-26 52 views
0

很多jQuery插件都支持AJAX,例如autocomplete jQuery UI pluginvalidation plugin帶Flask的jQuery AJAX插件

最爲牽掛的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框架完成的?我必須返回哪種類型的對象?

在此先感謝!

回答

2

配置插件以使用您應用的某個網址,例如, 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)