【问题标题】:How to send none input element from html to python如何将无输入元素从html发送到python
【发布时间】:2017-08-07 13:56:11
【问题描述】:

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

$(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 数组?

【问题讨论】:

    标签: javascript python jinja2


    【解决方案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 "====================" 
    

    【讨论】:

    • 非常感谢,我会测试它并告诉你结果,谢谢!
    • 我尝试按照您的回答进行操作,但是仍然无法正常工作。从控制台我得到了127.0.0.1 - - [08/Aug/2017 19:46:28] "GET /url/updateMenu?{%22FormDesc%22:[%22Account%22,%22Account%20Report%22,%22Account%20Drawdown%22]} HTTP/1.1 " 400 -。我认为它已经将数组解析为 JSON 格式,但无法在 view.py 中检索到。谢谢
    • 我终于解决了程序。根据您的回答,我尝试将$.ajax 方法类型更改为POST,如果方法是POST,则从html 中获取打印发送的变量,例如:if request.method == 'POST': getDesc = request.json['FormDesc'] print "Form Description is 1 ", getDesc。然后在控制台中,我得到了如下列表的结果:Form Description is 1 [u'Account', u'Account Report', u'Account Drawdown']。无论如何,您能否相应地稍微更新您的答案以使其适合?我会接受它作为唯一的答案。再次感谢,对我的解决方案真的很有帮助:)
    猜你喜欢
    • 2019-10-20
    • 2021-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-17
    相关资源
    最近更新 更多