如何从html发送无输入元素到python

时间:2017-08-07 13:56:12

标签: javascript python jinja2

我的脚本的一部分,我有如下的HTML代码:

$(document).ready(function(){
$("#Save").click(function(){
var FormDesc = ['Account','Account Report','Account Drawdown'];
            $.ajax({
                url:'/url/updateMenu',
                type:'GET',
                data:{FormDesc:FormDesc}
            }).success(function(data){
                console.log('data return');
               console.log(data);
            })
});
})

在我的view.py中,我尝试检索变量FormDesc以进行进一步处理,我的脚本就像这样。

from flask                      import request, url_for, render_template,flash,redirect,session, flash
from ..                         import app, db
from jinja2                     import Template

@app.route("/url/updateMenu", methods=['GET','POST'])
# @checkLogOutSession
# @checkLogOutTime
def getMenuToUpdate():

    getDesc      = request.args.get('FormDesc') if 'FormDesc' in request.args else 'Not received get Form Desc'

    print "===================="
    print "Form Description is ", getDesc
    print "====================" 

但是,我无法检索变量FromDesc。从控制台,它显示如下结果:

 * Debugger is active!
 * Debugger PIN: 332-799-490
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
====================
Form Description is 1  Not received get Form Desc
====================
====================
====================
127.0.0.1 - - [07/Aug/2017 20:52:21] "GET /url/MenuBuilder HTTP/1.1" 200 -
====================
Form POST Description is 1  Not received get Form Desc
====================
====================
127.0.0.1 - - [07/Aug/2017 20:52:25] "GET /url/updateMenu?FormDesc%5B%5D=Account&FormDesc%5B%5D=Account+Report&FormDesc%5B%5D=Account+Drawdown
HTTP/1.1" 200 -

如何从html文件中获取FormDesc数组? 谢谢你提前。

1 个答案:

答案 0 :(得分:1)

使用json更好。

$(document).ready(function(){
$("#Save").click(function(){
var FormDesc = ['Account','Account Report','Account Drawdown'];
            $.ajax({
                url:'/url/updateMenu',
                type:'GET',
                data:JSON.stringify({'FormDesc':FormDesc}),
                contentType : "application/json",
            }).success(function(data){
                console.log('data return');
               console.log(data);
            })
});
})

将view.py文件更改为

@app.route("/url/updateMenu", methods=['GET','POST'])
# @checkLogOutSession
# @checkLogOutTime
def getMenuToUpdate():
    getDesc = request.json['FormDesc']
    print "===================="
    print "Form Description is ", getDesc
    print "===================="