Python 如何从烧瓶中的“ImmutableMultiDict”获取数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29091070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get data from 'ImmutableMultiDict' in flask
提问by Suraj Palwe
I am learning how to use ajax and Flask ,so what I do is I send a ajax request and I receive the data as postrequest in my python file
我正在学习如何使用 ajax 和 Flask,所以我要做的是发送一个 ajax 请求,然后post在我的 python 文件中接收数据作为请求
My html file contains this code
My html file contains this code
var data = {"name":"John Doe","age":"21"};
$.ajax({
url:'/post/data',
datatype : "json",
contentType: "application/json; charset=utf-8",
data : JSON.stringify(data),
success : function(result) {
jQuery("#clash").html(result);
},error : function(result){
console.log(result);
}
});
And My python file contains :
我的python文件包含:
@app.route('/post/data',methods=['GET','POST'])
def postdata():
#do some
data = str(request.args)
json_dumps = json.dumps(data)
return json_dumps
This gives me following data on the page
这给了我页面上的以下数据
"ImmutableMultiDict([('{\"name\":\"John Doe\",\"age\":\"21\"}', u'')])"
And this is what my request.query_stringlooks {%22name%22:%22John%20Doe%22,%22age%22:%2221%22}
这就是我的request.query_string样子{%22name%22:%22John%20Doe%22,%22age%22:%2221%22}
So how do I get the nameand age. Please correct me If I am wrong anywhere.Thanks in advance.
那么我如何获得name和age. 如果我在任何地方错了,请纠正我。提前致谢。
采纳答案by Jason Brooks
You don't actually need to get data from an ImmutableMultiDict. There are a couple of errors in what you have that are preventing you from just pulling the response as json data. First off, you have to slightly tweak the parameters of your ajax call. You should add in the call type as a POST. Furthermore, datatypeshould be spelt as dataType. Your new call should be:
您实际上并不需要从ImmutableMultiDict. 您所拥有的有几个错误阻止您将响应作为 json 数据提取。首先,您必须稍微调整 ajax 调用的参数。您应该将呼叫类型添加为POST. 此外,datatype应拼写为dataType. 您的新电话应该是:
var data = {"name":"John Doe","age":"21"};
$.ajax({
type: 'POST',
contentType: 'application/json',
url: '/post/data',
dataType : 'json',
data : JSON.stringify(data),
success : function(result) {
jQuery("#clash").html(result);
},error : function(result){
console.log(result);
}
});
The data is now actually being sent as a post request with the jsontype. On the Flask server, we can now read the data as son information as follows:
数据现在实际上是作为具有该json类型的 post 请求发送的。在 Flask 服务器上,我们现在可以将数据读取为子信息,如下所示:
@app.route('/post/data',methods=['GET','POST'])
def postdata():
jsonData = request.get_json()
print jsonData['name']
print jsonData['age']
return "hello world" #or whatever you want to return
This will print John Doeand 21successfully.
这将打印John Doe并21成功。
Let me know if this works for you or if you have any additional questions!
让我知道这是否适合您,或者您有任何其他问题!
Edit: You can return success to the ajax call from flask as follows:
编辑:您可以将成功从flask 返回到ajax 调用,如下所示:
# include this import at the tomb
from flask import jsonify
@app.route('/post/data',methods=['GET','POST'])
def postdata():
...
return jsonify(success=True, data=jsonData)
回答by Magnus Tvedt
I came to this page because I'm trying to send a form with AJAX, and I finally found a solution. And the solution is to skip JSON (hope this will help others on the same search):
我来到这个页面是因为我试图用 AJAX 发送一个表单,我终于找到了一个解决方案。解决方案是跳过 JSON(希望这会帮助其他人进行相同的搜索):
$.ajax({
type: "POST",
url: my_url,
data: $("#formID").serialize(), //form containing name and age
success: function(result){
console.log(result);
}
});
Then, on the Flask server:
然后,在 Flask 服务器上:
app.route('/my_url', methods = [POST])
def some_function():
name = request.form['name']
age = request.form['age']
# do what you want with these variables
return 'You got it right'
回答by seanbehan
Just call to_dict on the request.form object E.g., http://www.seanbehan.com/how-to-get-a-dict-from-flask-request-form/
只需在 request.form 对象上调用 to_dict 例如,http://www.seanbehan.com/how-to-get-a-dict-from-flask-request-form/

