如何从烧瓶中的 'ImmutableMultiDict' 获取数据
how to get data from 'ImmutableMultiDict' in flask
我正在学习如何使用 ajax 和 Flask,所以我所做的是发送一个 ajax 请求,然后在我的 [=33] 中接收数据作为 post
请求=] 文件
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);
}
});
我的 python 文件包含:
@app.route('/post/data',methods=['GET','POST'])
def postdata():
#do some
data = str(request.args)
json_dumps = json.dumps(data)
return json_dumps
这为我提供了页面上的以下数据
"ImmutableMultiDict([('{\"name\":\"John Doe\",\"age\":\"21\"}', u'')])"
这就是我的 request.query_string
的样子 {%22name%22:%22John%20Doe%22,%22age%22:%2221%22}
那么我如何获得 name
和 age
。如果我错了,请提前纠正我anywhere.Thanks。
您实际上不需要从 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);
}
});
数据现在实际上是作为 post 类型的 json
请求发送的。在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
这将成功打印 John Doe
和 21
。
让我知道这是否适合您,或者如果您有任何其他问题!
编辑:您可以 return 成功调用来自 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)
我来到这个页面是因为我想用 AJAX 发送一个表单,我终于找到了解决办法。解决方案是跳过 JSON(希望这对同一搜索中的其他人有所帮助):
$.ajax({
type: "POST",
url: my_url,
data: $("#formID").serialize(), //form containing name and age
success: function(result){
console.log(result);
}
});
然后,在 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'
只需在 request.form 对象上调用 to_dict 例如,http://www.seanbehan.com/how-to-get-a-dict-from-flask-request-form/
我通过将 contentType 添加为 application/JSON 解决了这个问题
data ={
type:'POST',
contentType:'application/json',
otherData: 'foo'
}
您现在可以像这样访问 Flask 后端中的数据:
app.route('/my_url', methods = ["POST"])
def some_function():
other_data = request.form['otherData']
# do something
注意:我使用原版 JavaScript 而不是 jQuery
我正在学习如何使用 ajax 和 Flask,所以我所做的是发送一个 ajax 请求,然后在我的 [=33] 中接收数据作为 post
请求=] 文件
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);
}
});
我的 python 文件包含:
@app.route('/post/data',methods=['GET','POST'])
def postdata():
#do some
data = str(request.args)
json_dumps = json.dumps(data)
return json_dumps
这为我提供了页面上的以下数据
"ImmutableMultiDict([('{\"name\":\"John Doe\",\"age\":\"21\"}', u'')])"
这就是我的 request.query_string
的样子 {%22name%22:%22John%20Doe%22,%22age%22:%2221%22}
那么我如何获得 name
和 age
。如果我错了,请提前纠正我anywhere.Thanks。
您实际上不需要从 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);
}
});
数据现在实际上是作为 post 类型的 json
请求发送的。在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
这将成功打印 John Doe
和 21
。
让我知道这是否适合您,或者如果您有任何其他问题!
编辑:您可以 return 成功调用来自 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)
我来到这个页面是因为我想用 AJAX 发送一个表单,我终于找到了解决办法。解决方案是跳过 JSON(希望这对同一搜索中的其他人有所帮助):
$.ajax({
type: "POST",
url: my_url,
data: $("#formID").serialize(), //form containing name and age
success: function(result){
console.log(result);
}
});
然后,在 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'
只需在 request.form 对象上调用 to_dict 例如,http://www.seanbehan.com/how-to-get-a-dict-from-flask-request-form/
我通过将 contentType 添加为 application/JSON 解决了这个问题
data ={
type:'POST',
contentType:'application/json',
otherData: 'foo'
}
您现在可以像这样访问 Flask 后端中的数据:
app.route('/my_url', methods = ["POST"])
def some_function():
other_data = request.form['otherData']
# do something
注意:我使用原版 JavaScript 而不是 jQuery