通过Flask发送POST数据 [英] Sending POST Data through Flask

查看:1211
本文介绍了通过Flask发送POST数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让一些JavaScript运行一个函数来更新一个合理的大型json字典的表单输入的值,然后我想在我的瓶子后端得到它的持有。所以我有下面的HTML和JavaScript:

 < head> 
<! - 这里有一些信息 - >
< script>
函数AddPostData(){
var data = editor.getValue();
//这个editor.getValue()获取json字符串,它正在工作
//通过提示信息验证

var formInfo = document.forms ['json_form' ]。
formInfo.elements [json_data]。value = data;
alert(data)
}
< / script>
< / head>
< body>

<! - 我在这里有json编辑GUI - >

< form id =json_formmethod =POSTaction =/ test>
< input type =hiddenname =json_datavalue =/>
< input type =submitvalue =Submitonclick =AddPostData();>
< / form>

然后这里是我用于Flask的Python。

  @ app.route('/ test',methods = ['GET','POST'])
def test():
如果request.method =='POST':
print(request.data)
the_json = request.data
#这个模板简单地打印出来,所有我得到的是b
return render_template('testing.html',the_json = the_json)

?当我将一个字符串硬编码到数据变量中时,我仍然没有在request.data字段中得到任何东西,那么确切的说,我得到ab后面跟着一个引号:b



我想这是错误的,因为它实际上并没有将json放入输入的值字段中。

解决方案

< pre $ the_json = request.form.get('json_data',None)

而不是

  the_json = requ est.data 

将在硬编码部分工作(当然,但我不太确定如果你的Java脚本工作/不是我的专业领域,但它看起来没问题。)。

I am trying to have some javascript that runs a function to update the value of a form input with a reasonably large json dictionary and then I want to get a hold of it on my flask backend. So I have the follow HTML and javascript:

<head>
<!--some info here -->
<script>
        function AddPostData() {
            var data = editor.getValue();
            //this editor.getValue() gets the json string and it is working 
            //as validated by an alert message

            var formInfo = document.forms['json_form'];
            formInfo.elements["json_data"].value = data;
            alert(data)
        }
    </script>
  </head>
  <body>

    <!-- I have the json editing GUI here -->

    <form id="json_form" method="POST" action="/test">
        <input type="hidden" name="json_data" value="" />
        <input type="submit" value="Submit" onclick="AddPostData();">
    </form>

And then here is the python that I have for Flask.

@app.route('/test', methods=['GET', 'POST'])
def test():
    if request.method == 'POST':
        print (request.data)
        the_json=request.data
        #this template simply prints it out and all that I get is b"
        return render_template('testing.html', the_json=the_json)

Any ideas what is going wrong? When I hardcode a string into the data variable I still don't get anything in the request.data field. Well to be exact I get a b followed by one quotation: b"

I suppose it is something wrong in that it isn't actually putting the json into the value field of the input.

解决方案

the_json = request.form.get('json_data', None)

instead of

 the_json=request.data

will work in the hard coded section (for sure, but I'm not too sure if your java script works/not my area of expertise but it looks ok.).

这篇关于通过Flask发送POST数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆