如何让在瓶中从“ImmutableMultiDict”数据 [英] how to get data from 'ImmutableMultiDict' in flask
问题描述
我正在学习如何使用Ajax和瓶,所以我做的是我送一个Ajax请求,我收到我的Python文件数据为后
请求P>
我的HTML文件中包含该code
VAR数据= {名:李四,时代:21};
$阿贾克斯({
网址:'/后/数据,
数据类型:JSON,
的contentType:应用/ JSON的;字符集= UTF-8,
数据:JSON.stringify(数据),
成功:函数(结果){
jQuery的(#冲突)HTML(结果)。
}错误:函数(结果){
执行console.log(结果);
}
});
和我的Python文件包括:
@ app.route('/后/数据',方法=GET,POST])
高清POSTDATA():
#do一些
数据= STR(request.args中)
json_dumps = json.dumps(数据)
返回json_dumps
这给了我下面的页面上的数据。
ImmutableMultiDict([('{\名称\:\李四\,\年龄\:\21 \}',U'') ])
这是我的 request.query_string
的外观 {%22name%22:%22John%20Doe%22,%22age%22:% 2221%22}
那么,如何获得名称
和年龄
。请纠正我,如果我错了anywhere.Thanks提前。
您实际上并不需要从 ImmutableMultiDict
获取数据。有什么你是$ P $从刚才拉响应,JSON数据pventing你一对夫妇的错误。首先,你需要稍微调整你的AJAX调用的参数。你应该在呼叫类型添加为 POST
。此外,数据类型
应拼写为的dataType
。你的新的呼叫应该是:
VAR数据= {名:李四,时代:21};
$阿贾克斯({
键入:POST,
的contentType:应用/ JSON的,
网址:'/后/数据,
数据类型:JSON,
数据:JSON.stringify(数据),
成功:函数(结果){
jQuery的(#冲突)HTML(结果)。
}错误:函数(结果){
执行console.log(结果);
}
});
该数据现在实际上是被发送的与 JSON
键入一个POST请求。在瓶服务器,现在我们可以读取数据的子信息如下:
@ app.route('/后/数据',方法=GET,POST])
高清POSTDATA():
jsonData = request.get_json()
打印jsonData ['名称']
打印jsonData ['年龄']
返回的Hello World任何你想要#or返回
这将打印李四
和 21
成功。
让我知道这对你的作品,或者如果您有任何其他的问题!
编辑:您可以从瓶中如下返回成功的Ajax调用:
#包括此导入在墓
从瓶进口jsonify
@ app.route('/后/数据',方法=GET,POST])
高清POSTDATA():
...
返回jsonify(成功= TRUE,数据= jsonData)
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 post
request in my python file
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 :
@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_string
looks {%22name%22:%22John%20Doe%22,%22age%22:%2221%22}
So how do I get the name
and age
. Please correct me If I am wrong anywhere.Thanks in advance.
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, datatype
should be spelt as dataType
. Your new call should be:
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 json
type. On the Flask server, we can now read the data as son information as follows:
@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 Doe
and 21
successfully.
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:
# 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)
这篇关于如何让在瓶中从“ImmutableMultiDict”数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!